Django Model with left outer join

Trigger

Note that it was difficult to find information on Left outer join in Django's ORM.

Speaking of joins in Django's ORM

I often use select_related () and prefetch_related () to pull items that the model specifies as foreign keys.

class User(models.Model):
    name = models.CharField()

class Comment(models.Model):
    user = models.ForeignKey(User, null=True)
    content = models.CharField()

For example, if such a model is defined, it can be pulled as follows.

comment = Comment.objects.select_related().first()
print(comment.user.name)

It seems that reverse lookup is possible if only one data is used as a reference.

comments = User.objects.get(pk=1).comment_set.all()
# id=You can extract a comment that holds 1 User

If you do something like ↑, you'll find that Django joins are mostly Inner joins. What do you do with outer join after starting to operate the aggregation system? It will be.

left outer join method

comments = Comment.objects.filter(user__isnull=True)

That's right.

Summary

I checked it once but forgot it, so I wrote it down. It is important that the items are declared with models.ForeignKey () to combine. (Otherwise you can't do something like comment.user.name)

Recommended Posts

Django Model with left outer join
Automatically generate model relationships with Django
Django model: ManyToManyField
Internationalization with django
CRUD with Django
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Model fitting with lmfit
Send email with Django
Model changes in Django
High Performance Django --Model
File upload with django
Regression with linear model
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
[Django] Manage settings like writing in settings.py with a model
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Wai "Can Django do JOIN?"
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Do AES encryption with DJango
Use django model from interpreter
Getting Started with Python Django (6)
Calibrate the model with PyCaret
Combine two images with Django
Getting Started with Django with PyCharm
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django
django-rest-framework Django Model prevents simultaneous data updates with PostgreSQL optimistic concurrency