Django中的UUIDField(错误:运算符不存在:integer = uuid)

时间:2020-02-13 10:37:43

标签: django postgresql django-models uuid

我想使用uuid字段作为我的id(主键),但是它有问题,我无法修复...

这是我的模特

class Course(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=90)
    description = models.TextField()
    author = models.CharField(max_length=60)
    image = models.ImageField(upload_to='courses/images')
    intro_video = models.FileField(upload_to='courses/videos')
    free = models.BooleanField(default=False)
    price = models.CharField(max_length=60, blank=True)
    completed = models.BooleanField(default=False)
    duration = models.CharField(max_length=30, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

当我尝试添加一门新课程时,这是我遇到的错误

ProgrammingError at /admin/courses/course/add/
operator does not exist: integer = uuid
LINE 1: ..., "created_at" = NULL WHERE "courses_course"."id" = 'a130811...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Exception Type: ProgrammingError
Exception Value: operator does not exist: integer = uuid

我的数据库是PostgreSQL

请帮助我。

1 个答案:

答案 0 :(得分:0)

使用:

id=models.CharField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)