简体   繁体   English

处理自指外键; django搅拌机

[英]dealing with self-referential foreign keys; django mixer

I am getting the typical runtime error of self referencing FKs testing django with fake objects: 我收到了使用伪对象自引用FK测试django的典型运行时错误:

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. 文档很少将自己称为FK。 How can you fake an instance that requires on one of itself with django mixer? 您如何使用django混合器伪造一个需要自身实例的实例? Thank you 谢谢

Try adding null=True to the declaration of the parent field. 尝试将null=True添加到parent字段的声明中。 There has to be at least one top-level ItemGroup that won't have a parent. 必须至少有一个没有父项的顶级ItemGroup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM