Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django - Relationship

Back


Many-to-one relationship

from django.db import models

class Manufacturer(models.Model):
    pass


class Car(models.Model):
    # it is suggested that the name of a foreign key field be the name of the model
    manufacturer = models.ForeignKey(
        Manufacturer,
        on_delete=models.CASCADE
    )


    # it it fine that the name of a foreign key field uses a different name
    company_that_makes_it = models.ForeignKey(
        Manufacturer,
        on_delete=models.CASCADE,
    )


Many-to-many relationships

from django.db import models

class Topping(models.Model):
    pass

class Pizza(models.Model):
    # a Topping can be on multiple pizzas and each Pizza has multiple toppings
    toppings = models.ManyToManyField(Topping)

# In the above example, toppings is in Pizza (rather than Topping having a pizzas ManyToManyField ) because it’s more natural to think about a pizza having toppings than a topping being on multiple pizzas. The way it’s set up above, the Pizza form would let users select the toppings.

Intermediate Model

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name

# a group may have multiple persons. Relationship to person is created by intermediate model
class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through="Membership")  # using through argument target on intermediate model

    def __str__(self):
        return self.name

# intermediate model to represent complex relationship between persons and grouops, using ForeignKey
class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

One-to-one relationships

class MySpecialUser(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    supervisor = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="supervisor_of",
    )

TOP