【发布时间】:2017-02-05 02:42:51
【问题描述】:
这是我的模型
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
import uuid
class PiO(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # surrogate
person = models.ForeignKey(Person, on_delete=models.PROTECT, max_length=25, blank=True)
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT) # for the various organization types
object_id = models.UUIDField(primary_key=False, default=uuid.uuid4, editable=False) # the uuid of the specific org
content_object = GenericForeignKey('content_type', 'object_id')
这是我的回溯
AttributeError: 'UUIDField' object has no attribute 'uuid4'.
请注意,这是专门引用 object_id 字段,不是 uuid (pk) 字段。作为测试,我注释掉了 object_id 字段。我确实没有因为没有 object_id 字段而收到错误,然后检查在 12 行之外又出现了一个新错误。
我搜索了确切的短语并得到了
No results found for "AttributeError: 'UUIDField' object has no attribute 'uuid4'".
在我看来,我所做的与the docs 一致。
我错过了什么?通用外键和/或内容类型的存在与它有什么关系吗?
【问题讨论】:
标签: django python-2.7 django-1.9