ForeignKey('Owner', on_delete=cascade)
ForeignKey 참조할때, 'Owner'
로도 할 수있고, owner
도 할 수 있는데, 클래스 참조는 string으로 할 것을 권장한다.
정참조와 역참조 객체 호출
_set
또는 related_name
으로 접근# 역참조 _set
dogs = owner.dog_set.all()
# 역참조 related_name(FK에 지정)
dogs = owner.dogs.all()
through =
에 지정해준다.중간 테이블 객체를 가져옴.
actors = Actor.objects.all()
for actor in actors:
actors_movies = ActorMovie.objects.filter(actor_id = actor.id)
movie_list = []
for actor_movie in actors_movies:
movie_list.append({
'id' : actor_movie.movie.id,
'title' : actor_movie.movie.title,
})
# ...
Movie > ActorMovie로 가서 조회
actors = Actor.objects.all()
for actor in actors:
movies = Movie.objects.filter(actormovie__actor_id = actor.id)
movie_list = []
for movie in movies:
movie_list.append({
'id' : movie.id,
'title' : movie.title,
})
# ...
actors = Actor.objects.all()
for actor in actors:
movies = actor.movies.all()
movie_list = []
for movie in movies:
movie_list.append({
'id' : movie.id,
'title' : movie.title,
})
# ...