TIL|Django|M 2 M貫通例


M 2 M貫通使用例
from django.db import models

class Actor(models.Model):
    first_name = models.CharField(max_length=45)
    last_name = models.CharField(max_length=45)
    date_of_birth = models.DateField()

    class Meta:
        db_table = 'actors'

class Movie(models.Model):
    title = models.CharField(max_length=45)
    release_date = models.DateField()
    running_time = models.IntegerField()
    actors = models.ManyToManyField(
        "Actor",
        through="Actor_Movie_Joined",
        through_fields=("movie", "actor"),
    )

    class Meta:
        db_table = 'movies'

class Actor_Movie_Joined(models.Model):
    actor = models.ForeignKey("Actor", on_delete=models.CASCADE)
    movie = models.ForeignKey("Movie", on_delete=models.CASCADE)

    class Meta:
        db_table = 'actors_movies'