I am getting the typical runtime error of self referencing FKs testing django with fake objects:
In [12]: from mixer.backend.django import mixer
In [13]: x = mixer.blend(ItemGroup)
/home/cchilders/.virtualenvs/clientsite/lib/python3.4/site-packages/django/db/models/fields/related.py in __set__(self, instance, value)
587 raise ValueError(
588 'Cannot assign None: "%s.%s" does not allow null values.' %
--> 589 (instance._meta.object_name, self.field.name)
590 )
591 elif value is not None and not isinstance(value, self.field.rel.to):
ValueError: Mixer (<class 'clientsite.gacl.models.AroGroup'>): Cannot assign None: "AroGroup.parent" does not allow null values.
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 django mixer? Thank you
Try adding null=True
to the declaration of the parent
field. There has to be at least one top-level ItemGroup
that won't have a parent.
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.