Django dumpdata and loaddata not working for many-to-many intermediary model

吃可爱长大的小学妹 提交于 2019-12-12 06:09:06

问题


I am using dumpdata with Django 1.2.3 on the following model:

class Bar(models.Model):
    ...

class Foo(models.Model):
    bars = models.ManyToManyField(Bar, through="Foo_bar", blank=True, null=True)
    ...

class Foo_bar(models.Model):
    foo = models.ForeignKey(Foo)
    bar = models.ForeignKey(Bar)
    status = models.IntegerField()
    ...

The json fixture serializes the bars associated with Foos in the Foo objects, resulting in an AttributeError when I try to run loaddata with the fixture:

AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model.  Use App.Foo_bar's Manager instead.

Based on what I've read, dumpdata may have been fixed to not serialize m2m, or loaddata fixed to properly deal with them, but it doesn't seem so. I've tried the --natural flag, still no luck. Any ideas?

Thanks in advance.


回答1:


Depending on what you need to do with your fixtures, the command "dumpscript" from the django_extension package is really useful for handling fixture with complex relations.
No primary keys are used in the file, it's just a python script which only uses objects, and as such can re-create your whole db simply using object.save() calls.



来源:https://stackoverflow.com/questions/4698641/django-dumpdata-and-loaddata-not-working-for-many-to-many-intermediary-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!