python,django,django-queryset,timedelta,django-aggregation
As noted in the documentation, arithmetic with a DurationField will not always work as expected in databases other than PostgreSQL. I don't know to which extend this works or doesn't work in other databases, so you'll have to try it yourself. If that works, you can use the following query:...
python,django,django-models,django-aggregation
Just discovered that Django 1.8 has new conditional expressions feature, so now we can do like this: events = Event.objects.all().annotate(paid_participants=models.Sum( models.Case( models.When(participant__is_paid=True, then=1), default=0, output_field=models.IntegerField() ))) ...
sql,django,django-models,django-orm,django-aggregation
Opened Django compiler: def collapse_group_by(self, expressions, having): # If the DB can group by primary key, then group by the primary key of # query's main model. Note that for PostgreSQL the GROUP BY clause must # include the primary key of every table, but for MySQL it is enough...
yes, you can! from django.db.models import Count voting_round_instance.vote_set.values('vote') \ .annotate(count=Count('vote')).distinct() EDIT : use order_by() You may also need to make sure the default ordering does not mess up your aggregation. This is especially true when using related object managers. https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#interaction-with-default-ordering-or-order-by Fields that are mentioned in the order_by() part of a...
mysql,django,django-orm,django-aggregation
I think you must go with .raw because using .extra wont be possible here. The problem is because Django don't have .group_by the only way go group by something is to use .values and .annotate after that. (as you have done it in the first attempt) so.. why you can't...