Reputation: 1805
With the following models.py ...
from django.db import models
class Notebook(models.Model):
name = models.CharField(max_length=255)
class Entry(models.Model):
notebook = models.ForeignKey(Notebook, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
How would I write a filter to only return Notebook that have no Entry?
Upvotes: 0
Views: 25
Reputation: 476584
You can .filter(…)
the Notebook
s with:
Notebook.objects.filter(entry=None)
This works because Django makes a LEFT OUTER JOIN
, and thus we only retain the records for which the entry
primary key is NULL
/None
.
Upvotes: 1