[英]dealing with self-referential foreign keys; model mommy
I am getting the typical runtime error of self referencing FKs testing django with fake objects:我收到了自引用 FK 使用假对象测试 django 的典型运行时错误:
... last 4 frames repeated, from the frame below ...
/home/cchilders/.virtualenvs/clientsite/lib/python3.4/site-packages/model_mommy/mommy.py in make(model, _quantity, make_m2m, **attrs)
96 return [mommy.make(**attrs) for i in range(_quantity)]
97 else:
---> 98 return mommy.make(**attrs)
99
100
RuntimeError: maximum recursion depth exceeded while calling a Python object
the model:该模型:
class ItemGroup(models.Model):
parent = models.ForeignKey('self', db_column='parent_id')
name = models.CharField(max_length=255)
value = models.CharField(max_length=255, unique=True)
class Meta:
db_table = u'item_groups'
def __str__(self):
return self.value
def __repr__(self):
return '<{} {}: {}>'.format(self.__class__.__name__, self.pk, self.value)
The docs are sparse on referencing yourself as FK.将自己称为 FK 的文档很少。 How can you fake an instance that requires on one of itself with modelmommy?
你怎么能用modelmommy伪造一个需要自己的实例? Thank you
谢谢
First of all, I think you should make the parent
field nullable;首先,我认为您应该使
parent
字段可以为空; parent = models.ForeignKey('self', db_column='parent_id', null=True, blank=True)
that means you will have a base object from which to start referencing. parent = models.ForeignKey('self', db_column='parent_id', null=True, blank=True)
这意味着您将拥有一个可以开始引用的基础对象。
With model mommy, you can first create a fake parent, then assign the parent to the child.使用模型妈妈,您可以先创建一个假父母,然后将父母分配给孩子。
parent_item_group = mommy.make(ItemGroup)
child_item_group = mommy.make(ItemGroup, parent=parent_item_group)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.