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

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

LEFT JOIN Django ORM

You can do this by following the backwards relation in the lookup. >>> qs = Department.objects.filter( … departmentvolunteer__isnull=True).values_list(‘name’, flat=True) >>> print(qs.query) SELECT “app_department”.”name” FROM “app_department” LEFT OUTER JOIN “app_departmentvolunteer” ON ( “app_department”.”id” = “app_departmentvolunteer”.”department_id” ) WHERE “app_departmentvolunteer”.”id” IS NULL Here are the docs on queries “Spanning multi-valued relationships”: https://docs.djangoproject.com/en/stable/topics/db/queries/#spanning-multi-valued-relationships

django select_related – when to use it

You are actually asking two different questions: 1. does using select_related actually creates performance overhead? You should see documentation about Django Query Cache: Understand QuerySet evaluation To avoid performance problems, it is important to understand: that QuerySets are lazy. when they are evaluated. how the data is held in memory. So in summary, Django caches … Read more