In Django filter statement what’s the difference between __exact and equal sign (=)?

There is no difference, the second one implies using the __exact. From the documentation: For example, the following two statements are equivalent: >>> Blog.objects.get(id__exact=14) # Explicit form >>> Blog.objects.get(id=14) # __exact is implied This is for convenience, because exact # lookups are the common case.

Aggregation of an annotation in GROUP BY in Django

Update: Since Django 2.1, everything works out of the box. No workarounds needed and the produced query is correct. This is maybe a bit too late, but I have found the solution (tested with Django 1.11.1). The problem is, call to .values(‘publisher’), which is required to provide grouping, removes all annotations, that are not included … Read more

How to execute a GROUP BY … COUNT or SUM in Django ORM?

We can perform a GROUP BY … COUNT or a GROUP BY … SUM SQL equivalent queries on Django ORM, with the use of annotate(), values(), the django.db.models‘s Count and Sum methods respectfully and optionally the order_by() method: GROUP BY … COUNT: from django.db.models import Count result = Books.objects.values(‘author’) .order_by(‘author’) .annotate(count=Count(‘author’)) Now result contains a … Read more

Django: Force select related?

You can create a custom manager, and simply override get_queryset for it to apply everywhere. For example: class MyManager(models.Manager): def get_queryset(self): return super(MyManager, self).get_queryset().select_related(‘foo’, ‘bar’) (Prior to Django 1.6, it was get_query_set).

Is django prefetch_related supposed to work with GenericRelation

If you want to retrieve Book instances and prefetch the related tags use Book.objects.prefetch_related(‘tags’). No need to use the reverse relation here. You can also have a look at the related tests in the Django source code. Also the Django documentation states that prefetch_related() is supposed to work with GenericForeignKey and GenericRelation: prefetch_related, on the … Read more

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