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

Default image for ImageField in Django’s ORM

I haven’t tried this, but I’m relatively sure you can just set it as a default in your field. pic = models.ImageField(upload_to=’blah’, default=”path/to/my/default/image.jpg”) EDIT: Stupid StackOverflow won’t let me comment on other people’s answers, but that old snippet is not what you want. I highly recommend django-imagekit because it does tons of great image resizing … Read more

How to use custom manager with related objects?

For completeness of the topic, Django 1.7 (finally) supports using a custom reverse manager, so you can do something like that (just copying from the django docs): from django.db import models class Entry(models.Model): objects = models.Manager() # Default Manager entries = EntryManager() # Custom Manager b = Blog.objects.get(id=1) b.entry_set(manager=”entries”).all()

prefetch_related for multiple Levels

Since Django 1.7, instances of django.db.models.Prefetch class can be used as an argument of .prefetch_related. Prefetch object constructor has a queryset argument that allows to specify nested multiple levels prefetches like that: Project.objects.filter( is_main_section=True ).select_related( ‘project_group’ ).prefetch_related( Prefetch( ‘project_group__project_set’, queryset=Project.objects.prefetch_related( Prefetch( ‘projectmember_set’, to_attr=”projectmember_list” ) ), to_attr=”project_list” ) ) It is stored into attributes with _list … Read more

Django Aggregation: Summation of Multiplication of two fields

With Django 1.8 and above you can now pass an expression to your aggregate: from django.db.models import F Task.objects.aggregate(total=Sum(F(‘progress’) * F(‘estimated_days’)))[‘total’] Constants are also available, and everything is combinable: from django.db.models import Value Task.objects.aggregate(total=Sum(‘progress’) / Value(10))[‘total’]

Django ORM – objects.filter() vs. objects.all().filter() – which one is preferred?

The method all() on a manager just delegates to get_queryset(), as you can see in the Django source code: def all(self): return self.get_queryset() So it’s just a way to get the QuerySet from the Manager. This can be handy to ensure that you’re dealing with a QuerySet and not a Manager, because MyModel.objects returns a … Read more