▶️ models.ManyToManyField(참조할 클래스 이름)
ex ) Movie - Actor (Many-to-Many관계)
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=45)
release_date = models.DateField()
running_time = models.IntegerField()
class Meta:
db_table='movies'
class Actor(models.Model):
first_name = models.CharField(max_length=45)
last_name = models.CharField(max_length=45)
date_of_birth = models.DateField()
movies = models.ManyToManyField(Movie)
class Meta:
db_table='actors'
▶️ Actor객체.movies.add(Movie객체)
▶️ 정참조
model에 선언해둔 변수 이름으로 접근
a.movies
▶️ 역참조
클래스명에 _set을 붙여 접근
m.actor_set