Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

django-select-related

Can I use select_related() with ManyToManyField on Django Models?

December 4, 2023 by Tarik

Django versions 1.4 and above have prefetch_related for this purpose. The prefetch_related method is similar to select_related, but does not do a database join. Instead, it executes additional database queries and does the joining in Python.

Categories python Tags django, django-models, django-select-related, python Leave a comment

A left outer reverse select_related in Django?

September 14, 2023 by Tarik

Starting from Django 1.4 prefetch_related does what you want. Parent.objects.prefetch_related(‘child_set’) Related(!) django docs : https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related.

Categories django Tags django, django-queryset, django-select-related, left-join Leave a comment

How to use ‘select_related’ with get_object_or_404?

July 4, 2023 by Tarik

Yes. obj = get_object_or_404(MyModel.objects.select_related(), whatever=whatever)

Categories django Tags django, django-models, django-select-related Leave a comment

Selecting specific fields using select_related in Django

May 6, 2023 by Tarik

select_related should be use on the whole model, and then you can filter it more. This will work: Articles.objects.select_related(‘blog’).only(‘blog__name’, ‘title’, ‘create_time’)

Categories django Tags django, django-1.8, django-queryset, django-select-related, python-2.7 Leave a comment

Django Query Related Field Count

February 10, 2023 by Tarik

You should use aggregates. from django.db.models import Count User.objects.annotate(page_count=Count(‘page’)).filter(page_count__gte=2).count()

Categories django Tags django, django-queryset, django-select-related Leave a comment

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa