Consider the two models below:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=255)
class Book(models.Model):
author = models.ForeignKey(Author, models.CASCADE, related_name="books")
how to get only authors that does not have books?
2
Answers
You could do something like this: fetch all the authors of books, and then fetch all the authors that you didn’t retrieve in the first query (meaning they have no books).
Untested code:
You can use isnull or just pass
None
to the related_name to perform this filter