如何将django uuid转换为注释的QuerySet中的整数?

发布于 2025-02-11 10:30:23 字数 803 浏览 0 评论 0原文

我正在尝试将UUID转换为注释中的整数。

因此:

Item.objects.values_list('pk', flat=True).annotate(
    int_of_pk=int('pk')
)

哪个引发错误:

valueerror:base 10:'pk' or for int()的文字无效

或类似:

from django.db.models import IntegerField
from django.db.models.functions import Cast

Item.objects.values_list('pk', flat=True).annotate(
    int_of_pk=Cast('pk', output_field=IntegerField())
)

哪个抛出错误:

File "/path/ve/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type uuid to integer
LINE 1: ...em"."uuid", ("item"."uuid")::integer ...
                                      ^

有什么想法弹出您吗?

I'm trying to convert UUIDs to integers within an annotate.

So like:

Item.objects.values_list('pk', flat=True).annotate(
    int_of_pk=int('pk')
)

which throws error:

ValueError: invalid literal for int() with base 10: 'pk'

or like:

from django.db.models import IntegerField
from django.db.models.functions import Cast

Item.objects.values_list('pk', flat=True).annotate(
    int_of_pk=Cast('pk', output_field=IntegerField())
)

which throws error:

File "/path/ve/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type uuid to integer
LINE 1: ...em"."uuid", ("item"."uuid")::integer ...
                                      ^

Any ideas pop out at you?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文