【问题标题】:query an object using uuid in django在 django 中使用 uuid 查询对象
【发布时间】:2017-10-21 21:15:48
【问题描述】:

我正在使用 uuid 创建一个 id 字段,它是如下的主键

import uuid

class User_Profile(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

因此,每当我们将对象保存到数据库中时,它都会保存为 UUID 实例而不是字符串,如下所示

user_profiles = User_Profile.objects.values_list('id', flat=True)
print user_profiles
[UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')]

现在如何使用 django ORM 查询它?因为它没有保存为字符串,所以我无法按如下方式获取它并出现错误

user_profile = User_Profile.objects.get(id='193b6acc-2b78-4ddc-9ef8-632cde33ef74')

错误:

ValueError: invalid literal for int() with base 10: '193b6acc-2b78-4ddc-9ef8-632cde33ef74'

当我从 Django 中的查询参数中收到此 uuid 作为字符串时,我还尝试将其从字符串转换为 uuid,如下所示,我得到了错误

import uuid
id = uuid.UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')
user_profile = User_Profile.objects.get(id=id)

错误:

ProgrammingError: operator does not exist: uuid = numeric
LINE 1: ...ifications" FROM "tc_user_profile" WHERE "tc_user_profile"."id" = 33539211...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

那么最后如何从 django 数据库中查询一个 uuid id 字段?

【问题讨论】:

    标签: python django django-queryset uuid


    【解决方案1】:

    该字段是一个 UUID,因此您应该传递一个 UUID 来查询它。如果您看到它,那就很简单了:

    import uuid
    id = uuid.UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')
    user_profile = User_Profile.objects.get(id=id)
    

    【讨论】:

    • 对不起,我没听懂,上面的代码,我收到一个错误HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
    【解决方案2】:

    我假设你使用 postgreSQL :

    Django 文档说:在 PostgreSQL 上使用时,它以 uuid 数据类型存储,否则以 char(32) 形式存储。

    https://docs.djangoproject.com/fr/1.11/ref/models/fields/

    【讨论】:

    • 是的,我是 postgres 数据库,但是为什么我无法获得结果并面临错误,我在上面的代码中做错了什么
    • 你用的是哪个版本的 Django?
    • 我用 Django==1.8.13
    • 1.8 版的 uuid 和 postgreSQL 有一些问题(PostgreSQLUUIDField -> UUIDField 在这个版本中),也许你需要升级你的 Django 并进行新的迁移:(
    【解决方案3】:

    实际上,我已经在我的机器上使用数据库 PostGres 进行了尝试,它可以工作,

    >>> user = User_Profile.objects.create()
    >>> user
    <User_Profile: User_Profile object>
    >>> user.id
    UUID('d92c2280-4682-42ea-86c3-a1ed993c0638')
    #copied the string of uuid only.
    >>> id = 'd92c2280-4682-42ea-86c3-a1ed993c0638'
    >>> User_Profile.objects.get(id=id)
    <User_Profile: User_Profile object>
    
    >>> import uuid
    #converted the string into UUID format
    >>> id = uuid.UUID(id)
    >>> id
    UUID('d92c2280-4682-42ea-86c3-a1ed993c0638')
    >>> User_Profile.objects.get(id=id)
    <User_Profile: User_Profile object>
    

    如果问题仍然存在,请尝试删除数据库和迁移并重新创建它。您面临的问题与数据库无关,可能是您的配置中的问题。

    【讨论】:

    • 您使用的是哪个数据库?就像我在使用 postgres
    • 目前我只是在 sqlite3 上对其进行了测试,让我也看看 postgres。我会在此之后尽快编辑答案。
    • 我在 postgres 数据库中也做过同样的事情,而且效果很好。
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 2021-09-15
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2013-12-11
    • 2021-10-26
    相关资源
    最近更新 更多