python - Django ORM: Min('field',filter=...) 导致 TypeError: can only concatenate list (not "tuple") to list
问题描述
有一个模型Location
可以有许多Ticket
对象(使用ForeignKey
)。
该Ticket
模型的price
字段是DecimalField
.
现在我已经过滤了对象的 QuerySet Ticket
,我想获取Location
对象的 QuerySet 并注释值,这是FILTERED QuerySet 中所有对象min_price
的最低价格。Ticket
例如:
tickets = Ticket.objects.filter(something)
locations = Location.objects.all().annotate(min_price=<minimal price from tickets having this location>)
我尝试了什么:
locations_annotated = Location.objects.all().annotate(
min_price=Min('tickets__min_price', filter=tickets))
这行不通。当我尝试从中获取第一个元素时locations_annotated
,调试器返回:
TypeError: can only concatenate list (not "tuple") to list
你知道该怎么做吗?
解决方案
我认为注释没有正确使用。请试试,
locations_annotated = Location.objects.annotate(
min_price=Min('tickets__min_price', filter=tickets)
推荐阅读
- django - 网址未解决,404错误django
- view - 浏览量
- javascript - 如何从 React 访问 Django REST 框架 API
- c# - 如何在cshtml中设置Label的ID和runat
- electron - 如何将电子 DeprecationWarning 视为错误
- mysql - mysql将字段值传递给其他选择查询
- javascript - 编辑向导最后一步中发送的消息 | 电报-js
- debian - 使用 fpm 移动配置文件
- html - 如何使绝对定位的元素在需要为窗口的 50% 时对齐中心
- arrays - VHDL Modelsim:数组长度不匹配(空数组与长度为 8 的数组)