[Django]-Django GROUP BY including unnecessary columns?

2👍

only only defers the loading of certain fields, i.e. it allows for lazy loading of big or unused fields. It should generally not be used unless you know exactly what you’re doing and why you need it, as it is nothing more than a performance booster than often decreases performance with improper use.

What you’re looking for is values() (or values_list()), which actually excludes certain fields instead of just lazy loading. This will return a dictionary (or list) instead of a model instance, but this is the only way to tell Django to not take other fields into account:

qs = (Result.objects.filter_by(organisation_id=1)
            .values('time').annotate(Count('id')))
👤knbk

Leave a comment