gpt4 book ai didi

python - Django: AttributeError: 'Q' 对象没有属性 'count'

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:55 24 4
gpt4 key购买 nike

我尝试统计所有与会者的数量。我创建了以下for循环查询集。但是,我总是收到错误 AttributeError: 'Q' object has no attribute 'count'。您知道如何解决这个问题吗?

# Get count of attendees per ticket and then combine these
tickets = Ticket.objects.filter(event=3)
count = 0
for ticket in tickets:
new_count = ticket.attendees.filter=Q(
canceled=False,
order__status__in=(
OrderStatus.PAID,
OrderStatus.PENDING,
OrderStatus.PARTIALLY_REFUNDED,
OrderStatus.FREE,
),
).count()
count += new_count
print(count)

最佳答案

您分配给 .filter:请注意,您编写的是 .filter=Q(,而不是 .filter(Q(...)).

您可以通过以下方式解决此问题:

tickets = Ticket.objects.filter(event=3)
count = 0
for ticket in tickets:
new_count = ticket.attendees<b>.filter</b>(
canceled=False,
order__status__in=(
OrderStatus.PAID,
OrderStatus.PENDING,
OrderStatus.PARTIALLY_REFUNDED,
OrderStatus.FREE,
)
).count()
count += new_count
print(count)

话虽如此,我认为您可以通过如下所示的单个调用更有效地处理此问题:

Attendee.objects.filter(
<b>ticket__event=3</b>,
canceled=False,
order__status__in=(
OrderStatus.PAID,
OrderStatus.PENDING,
OrderStatus.PARTIALLY_REFUNDED,
OrderStatus.FREE,
)
).count()

因此,我们在此计算持有 event=3门票参加者数量。这可能比进行 n 次查询更有效。

关于python - Django: AttributeError: 'Q' 对象没有属性 'count',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753070/

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