In Django, it's possible to have two models reference each other through a ForeignKey or a ManyToManyField. To demonstrate this, let's consider a simple example of two models, Author and Book, where each Author can write multiple Book objects, and each Book can have one Author. Here's how you can set up this relationship:
from django.db import models class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, on_delete=models.CASCADE) def __str__(self): return self.title
In this example:
We define the Author model with a name field, which represents the author's name.
We define the Book model with a title field and a ForeignKey field called author. The author field establishes a many-to-one relationship with the Author model. It means that each book has one author, and each author can have multiple books.
By using ForeignKey in the Book model, we establish a reference to the Author model. The on_delete=models.CASCADE argument ensures that when an author is deleted, all their associated books will also be deleted. You can customize the behavior by using other options like models.PROTECT, models.SET_NULL, or models.SET_DEFAULT, depending on your use case.
Now, you can create instances of these models and establish references between them like this:
# Create authors author1 = Author.objects.create(name="Author 1") author2 = Author.objects.create(name="Author 2") # Create books and link them to authors book1 = Book.objects.create(title="Book 1", author=author1) book2 = Book.objects.create(title="Book 2", author=author1) book3 = Book.objects.create(title="Book 3", author=author2) # Accessing related objects author_books = author1.book_set.all() # Get all books by Author 1 book_author = book1.author # Get the author of Book 1
This setup allows you to establish a bidirectional relationship between the Author and Book models, where you can access a book's author and an author's books easily.
How to establish a bidirectional relationship between two models in Django?
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) Django - How to create a Many-to-Many relationship between two models with mutual referencing?
ManyToManyField in both models, specifying the symmetrical attribute as False.from django.db import models class ModelA(models.Model): related_models = models.ManyToManyField('ModelB') class ModelB(models.Model): related_models = models.ManyToManyField(ModelA) How to define a bidirectional relationship with a OneToOneField in Django models?
from django.db import models class ModelA(models.Model): model_b = models.OneToOneField('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.OneToOneField(ModelA, on_delete=models.CASCADE) Django - How to model a circular reference between two models?
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) Best practices for modeling mutual referencing between Django models?
from django.db import models class ModelA(models.Model): related_models = models.ManyToManyField('ModelB') class ModelB(models.Model): related_models = models.ManyToManyField(ModelA) How to avoid circular dependency issues when defining mutual referencing in Django?
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) Implementing bidirectional relationships between Django models without causing migration conflicts
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) Django - How to create cross-referencing models without causing database integrity issues?
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) Handling recursive relationships between Django models effectively
symmetrical=False.from django.db import models class ModelA(models.Model): parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True) class ModelB(models.Model): parent = models.ManyToManyField('self', symmetrical=False) Django - How to establish mutual referencing between models without violating DRY principles?
from django.db import models class ModelA(models.Model): model_b = models.ForeignKey('ModelB', on_delete=models.CASCADE) class ModelB(models.Model): model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE) expandoobject awk provisioning-profile stack opensuse looker-studio extjs jenkins-agent jwplayer gallery