繁体   English   中英

如何在查询集注释中将 Django UUID 转换为整数?

[英]How to convert a Django UUID to integer within a queryset annotate?

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

就像:

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

引发错误:

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

或喜欢:

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 ...
                                      ^

你有什么想法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM