首页 > 解决方案 > 如何按名称和最高时间戳获取对象

问题描述

我想获取属于 external_id=1 并具有最高时间戳的事务。

我试过这个

max_rating = Transaction.objects.aggregate(organisation_id_from_partner=organisation_id, highest_timestamp=Max('timestamp'))

但我明白了TypeError: QuerySet.aggregate() received non-expression(s): 1.

标签: djangodjango-models

解决方案


在聚合内部,您只能编写聚合函数。尝试这个

max_rating = Transaction.objects.filter(organisation_id_from_partner=organisation_id).order_by('-timestamp').first()

推荐阅读