1👍
✅
You work with a ManyToManyField
[Django-doc] to represent relations where a movie can have multiple MovieCast
s and a MovieCast
can belong to multiple Movie
s.
For example:
class MovieCast(models.Model):
actor_name = models.CharField(max_length=100)
class Movie(models.Model):
name = models.CharField(max_length=128)
cast = models.ManyToManyField(
MovieCast,
related_name='movies'
)
We can then construct for example two movies and two MovieCast
objects:
titanic = Movie.objects.create(name='Titanic')
laborday = Movie.objects.create(name='Labor Day')
leonardo = MovieCast.objects.create(actor_name='Leonardo DiCaprio')
kate = MovieCast.objects.create(actor_name='Kate Winslet')
titanic.cast.add(leonardo, kate)
laborday.cast.add(kate)
For more information, see the many-to-many relationships section of the documentation.
Source:stackexchange.com