UUIDField没有属性uuid4

Mal*_*umi 0 django python-2.7 django-1.9

这是我的模特

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')
Run Code Online (Sandbox Code Playgroud)

这是我的追溯

AttributeError: 'UUIDField' object has no attribute 'uuid4'.
Run Code Online (Sandbox Code Playgroud)

请注意,这是特定引用object_id字段,而不是 uuid(pk)字段.作为测试,我注释掉了object_id字段.我并没有得到一个错误,不具有OBJECT_ID字段,检查12线走了,给到一个新的错误.

我用谷歌搜索了确切的短语并得到了

No results found for "AttributeError: 'UUIDField' object has no attribute 'uuid4'".
Run Code Online (Sandbox Code Playgroud)

我所做的看起来与我的文档一致.

我错过了什么?通用外键和/或内容类型的存在是否与它有关?

Ala*_*air 7

问题是您的模型字段uuid与模块发生冲突uuid.

一种选择是重命名模型字段,例如:

class PiO(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    ...
Run Code Online (Sandbox Code Playgroud)

另一种选择是将导入更改为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
Run Code Online (Sandbox Code Playgroud)