Django Notes 7: Relational Database Handling
- Johnas Lee
- Jun 10, 2020
- 1 min read
Many-to-One Relationship
In order to perform relation between different models, we can define the field by the following:
customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)The on_delete parameter states that the whole instance will be deleted automatically when the customer is deleted.
Many-to-Many Relationship
For the many-to-many relationship, we can define the field by the following:
tags = models.ManyToManyField(Tag)Then we can store multiple values for a corresponding type of object.


Comments