这是我的模特
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一致。
我错过了什么?通用外键和/或内容类型的存在是否与它有关?
答案 0 :(得分:6)
问题是您的模型字段uuid
与模块uuid
发生冲突。
一种选择是重命名模型字段,例如:
class PiO(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
...
另一种选择是将导入更改为from uuid import uuid4
,并更新默认值以使用uuid4
而不是uuid.uuid4
。
from uuid import uuid4
class PiO(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False) # surrogate
...
object_id = models.UUIDField(primary_key=False, default=uuid4, editable=False) # the uuid of the specific org