Django dumpdata和loaddata不能用于多对多中介模型 [英] Django dumpdata and loaddata not working for many-to-many intermediary model
问题描述
我在以下模型中使用了Django 1.2.3的dumpdata:
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()
...
json fixture将与Foo对象中的Foos,当我尝试使用fixture运行loaddata时,导致AttributeError:
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.
根据我的阅读,dumpdata可能已经被修复为不能序列化m2m,或者loaddata被修改为正确处理它们,但是似乎不是这样。我试过了 - 自然的旗帜,还是没有运气。任何想法?
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?
提前感谢
推荐答案
您需要使用固定装置,django_extension包中的命令 dumpscript 实际上是对于处理具有复杂关系的夹具很有用。
文件中没有使用主键,它只是一个只使用对象的python脚本,因此可以使用 object.save()
call。
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.
这篇关于Django dumpdata和loaddata不能用于多对多中介模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!