Django dumpdata和loaddata不适用于多对多中介模型

时间:2011-01-15 08:19:47

标签: django serialization model many-to-many

我在以下模型中使用带有Django 1.2.3的dumpdata:

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:

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

根据我read的内容,dumpdata可能已修复为不序列化m2m,或者loaddata已修复以正确处理它们,但似乎并非如此。我试过 - 自然的旗帜,仍然没有运气。有什么想法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

根据您需要对灯具执行的操作,django_extension包中的命令“dumpscript”对于处理复杂关系的灯具非常有用。
文件中没有使用主键,它只是一个只使用对象的python脚本,因此只需使用object.save()调用即可重新创建整个数据库。