how to join models from another app 1

how to join models from another app

from django.db import models
from geography.models import ZipCode

class Restaurant(models.Model):
    # ...
    zip_code = models.ForeignKey(
        ZipCode,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

Here is what the above code is Doing:
1. We’re importing the ZipCode model from the geography app.
2. We’re adding a ForeignKey field to the Restaurant model.
3. We’re setting the on_delete argument to models.SET_NULL. This means that if the ZipCode instance that the Restaurant is related to is deleted, the Restaurant instance will be set to null.
4. We’re setting blank=True and null=True. This means that the field can be left blank and it can be set to null.

Similar Posts