dumpdata: option to fetch related objects
|Reported by:||stanislas.guerra@…||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Just started to write a patch that add a --related option to the dumpdata command where you can specify the related objects you want to fetch aside the main one using --pks.
class Article(models.Model): headline = models.CharField(max_length=100, default='Default headline') pub_date = models.DateTimeField() tag = models.ForeignKey("Tag") class Tag(models.Model): name = models.CharField(max_length=100) class Blog(models.Model): name = models.CharField(max_length=100) featured = models.ForeignKey(Article, related_name='fixtures_featured_set') articles = models.ManyToManyField(Article, blank=True, related_name='fixtures_articles_set')
Allows you to dump several blogs, the articles and the tag on the articles.
python manage.py dumpdata myapp.Blog --pks=2,3 --related=articles.tag,articles,featured
This is very basic at the moment and there are some limitations:
- If you use the --natural switch, you have to be careful in the related order because objects are not sorted ;
- Objects can appear several times (in the previous example, if the featured article is also in the articles relation it will be present 2 times in the dump).
Change History (3)
comment:1 follow-up: ↓ 2 Changed 21 months ago by russellm
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Resolution set to duplicate
- Status changed from new to closed