gpt4 book ai didi

python - 如何注释 Django QuerySet 中两个字段的最大值

转载 作者:太空狗 更新时间:2023-10-29 21:33:27 26 4
gpt4 key购买 nike

我有一个模型Client,我如何注释然后排序,它的两个字段的最大值:

from django.db import models

class Client(models.Model):
uploaded_photo_at = models.DateTimeField()
uploaded_document_at = models.DateTimeField()

以下内容:

Client.objects.annotate(
latest_activity_at=Max('uploaded_photo_at', 'uploaded_document_at', output_field=DateTimeField())
).order_by('latest_activity_at')

引发此错误:

django.db.utils.ProgrammingError: function max(timestamp with time zone, timestamp with time zone) does not exist
LINE 1: ...oto_at", "clients_client"."uploaded_document_at", MAX("clien...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

如果有帮助的话,我正在使用 Posgresql 和 Django 1.11。

最佳答案

感谢罗伯特的回答,我找到了 Greatest class of Django .

以下作品:

from django.db.models.functions import Greatest

Client.objects.annotate(
latest_activity_at=Greatest('uploaded_photo_at', 'uploaded_document_at')
).order_by('latest_activity_at')

关于python - 如何注释 Django QuerySet 中两个字段的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46210138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com