I am getting the typical runtime error of self referencing FKs testing django with fake objects:
... 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. How can you fake an instance that requires on one of itself with modelmommy? Thank you
First of all, I think you should make the parent
field nullable; 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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.