For instance, we want to create an Address model, and we want each Customer to have an address and one address only. This means that our Address model will inherit from the Customer model. Therefore, the Customer model becomes the parent of the Address model. This is an example of a one-to-one relationship. See implementation below.
class Address(models.Model): street = models.charField(max_length=255) city = models.charField(max_length=255) customer = models.OneToOneField(Customer, on_delete=models.CASCADE, primary_key=True)In the above code, we have created the street and city fields for our model. Now we want to establish a relationship between the address and customer models. So we create a new field called “customer” and it’s field is a OneToOneField. The OneToOneField takes 3 arguments, the first one is the Class we want to inherit/form the relationship with. The second one is what happens if the parent model is deleted(in this case the CASCADE means that the address will be deleted if the Customer is deleted.), and the third one is to create a primary key so that each address has a unique primary key.
We dont have to go inside our parent class(Customer) to create a field called address and specify a reverse relationship with the Address, because Django does it automatically for you.