Why my queryset changes size after I evaluate it in Django?

Viewed 65

I have a simple model:

class Expense(models.Model):
    ...
    price = models.DecimalField(decimal_places=2, max_digits=6)
    is_fixed = models.BooleanField(default=False)

I'm trying a simple query to group_by and aggregate:

>>> from expenses.models import Expense
>>> from django.db.models import Sum
>>> qs = Expense.objects.values('is_fixed').annotate(total=Sum('price'))

I expected that qs will have two records:

>>> qs.count()
2

But when I evaluate it, it returns nine!

>>> len(qs)
9

After the evaluation, count starts to return nine as well:

>>> qs.count()
9

To ilustrate even more:

>>> 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

I have no idea what is causing this "anomaly". What do you think it might be?

UPDATE: Here are some data, as requested by 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
    }
]
1 Answers

The problem was because I had set ordering on the Meta:

class Meta:
    ordering = ("-created_at",)

Setting the correct field on the order_by clause fixed the issue:

>>> 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
Related