In one of my projects, I came across the need to have a relationship of one model with one of several fields ** of another model**. I do not know how to do this, because if I just refer from two attributes through the same Foreign Key using one related_name, I have errors
Here is a similar example. There is a table with films and their directors. And also each user can share his opinion and indicate one the best and one the worst movie in his opinion. At the same time, they should be different films.
The bottom line is that I want out of the Movie
model I could get all mentions of a certain movie. That is, movie.rates.all()
and would get a QuerySet of all the rates of this movie, the worst and the best.
How to implement it?
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=100)
rating = models.IntegerField()
date = models.DateField()
director = models.ForeignKey('Director', related_name='movies', on_delete=models.CASCADE, blank=True, null=True)
class Director(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class MovieRate(models.Model):
author_name = models.CharField(max_length=100)
# best =
# worst =