gpt4 book ai didi

python - 为什么我的查询集在 Django 中评估后会改变大小?

转载 作者:行者123 更新时间:2023-12-04 07:16:15 25 4
gpt4 key购买 nike

我有一个简单的模型:

class Expense(models.Model):
...
price = models.DecimalField(decimal_places=2, max_digits=6)
is_fixed = models.BooleanField(default=False)
我正在尝试对 group_by 和聚合进行简单查询:
>>> from expenses.models import Expense
>>> from django.db.models import Sum
>>> qs = Expense.objects.values('is_fixed').annotate(total=Sum('price'))
我预计 qs会有两条记录:
>>> qs.count()
2
但是当我评估它时,它返回九!
>>> len(qs)
9
经评价, count也开始返回九:
>>> qs.count()
9
进一步说明:
>>> qs = Expense.objects.values('is_fixed').annotate(total=Sum('price'))
>>> qs.count() == len(qs)
False
>>> len(qs) == qs.count()
True
>>> qs = Expense.objects.values('is_fixed').annotate(total=Sum('price'))
>>> len(qs) == qs.count()
True
我不知道是什么导致了这种“异常”。你认为它可能是什么?
更新:
以下是 bdbd 要求的一些数据:
[
{
"price": 9.11,
"category": "HOUSE",
"created_at": "14/08/2021",
"source": "BANK_SLIP",
"is_fixed": True
},
{
"price": 600.0,
"category": "GIFT",
"created_at": "09/08/2021",
"source": "BANK_TRANSFER",
"is_fixed": False
},
{
"price": 10.5,
"category": "FOOD",
"created_at": "08/08/2021",
"source": "SETTLE_UP",
"is_fixed": False
},
{
"price": 28.0,
"category": "GIFT",
"created_at": "08/08/2021",
"source": "SETTLE_UP",
"is_fixed": False
},
{
"price": 20.44,
"category": "SUPERMARKET",
"created_at": "07/08/2021",
"source": "SETTLE_UP",
"is_fixed": False
},
{
"price": 8.5,
"category": "SUPERMARKET",
"created_at": "06/08/2021",
"source": "CREDIT_CARD",
"is_fixed": False
},
{
"price": 726.0,
"category": "HOUSE",
"created_at": "04/08/2021",
"source": "BANK_SLIP",
"is_fixed": True
},
{
"price": 34.8,
"category": "HOUSE",
"created_at": "04/08/2021",
"source": "BANK_SLIP",
"is_fixed": True
},
{
"price": 43.97,
"category": "HOUSE",
"created_at": "04/08/2021",
"source": "MONEY",
"is_fixed": True
},
{
"price": 4.09,
"category": "SUPERMARKET",
"created_at": "04/08/2021",
"source": "CREDIT_CARD",
"is_fixed": False
},
{
"price": 168.0,
"category": "OTHER",
"created_at": "04/08/2021",
"source": "BANK_SLIP",
"is_fixed": False
},
{
"price": 23.4,
"category": "FOOD",
"created_at": "04/08/2021",
"source": "SETTLE_UP",
"is_fixed": False
},
{
"price": 169.0,
"category": "OTHER",
"created_at": "03/08/2021",
"source": "BANK_SLIP",
"is_fixed": False
},
{
"price": 9.81,
"category": "SUPERMARKET",
"created_at": "03/08/2021",
"source": "CREDIT_CARD",
"is_fixed": False
},
{
"price": 100.0,
"category": "RECREATION",
"created_at": "03/08/2021",
"source": "MONEY",
"is_fixed": False
},
{
"price": 10.0,
"category": "SUPERMARKET",
"created_at": "02/08/2021",
"source": "CREDIT_CARD",
"is_fixed": False
}
]

最佳答案

问题是因为我设置了 orderingMeta :

class Meta:
ordering = ("-created_at",)
在 order_by 子句上设置正确的字段解决了这个问题:
>>> qs = Expense.objects.order_by("is_fixed").values("is_fixed").annotate(total=Sum("price"))
>>> len(qs) == qs.count()
True
>>> qs = Expense.objects.order_by("is_fixed").values("is_fixed").annotate(total=Sum("price"))
>>> qs.count() == len(qs)
True

关于python - 为什么我的查询集在 Django 中评估后会改变大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68734419/

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