Django equivalent of COUNT with GROUP BY

If you are using Django 1.1 beta (trunk): Player.objects.values(‘player_type’).order_by().annotate(Count(‘player_type’)) values(‘player_type’) – for inclusion only player_type field into GROUP BY clause. order_by() – for exclusion possible default ordering that can cause not needed fields inclusion in SELECT and GROUP BY.

Django order_by() filter with distinct()

Based on your error message and this other question, it seems to me this would fix it: p = Product.objects.filter(vendornumber=”403516006″)\ .order_by(‘vendor__name’, ‘-created’).distinct(‘vendor__name’) That is, it seems that the DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). So by making the column you use in distinct as the first column in the order_by, I … Read more

Django: Group by date (day, month, year)

Django 1.10 and above Django documentation lists extra as deprecated soon. (Thanks for pointing that out @seddonym, @Lucas03). I opened a ticket and this is the solution that jarshwah provided. from django.db.models.functions import TruncMonth from django.db.models import Count Sales.objects .annotate(month=TruncMonth(‘created’)) # Truncate to month and add to select list .values(‘month’) # Group By month .annotate(c=Count(‘id’)) … Read more

How to filter objects for count annotation in Django?

Conditional aggregation in Django 2.0 allows you to further reduce the amount of faff this has been in the past. This will also use Postgres’ filter logic, which is somewhat faster than a sum-case (I’ve seen numbers like 20-30% bandied around). Anyway, in your case, we’re looking at something as simple as: from django.db.models import … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)