多对多关系-电影-角色-演员 [英] Many-to-Many Relationships - Movie - Role - Actor

查看:48
本文介绍了多对多关系-电影-角色-演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定描述此概念模型的最佳解决方案是什么.我知道如何建立多对多关系,但不知道如何在该关系中扮演角色.你能告诉我最好的方法是什么吗?

I'm not sure what is the best solution to describe this conceptual model. I know how to create a many to many relationship, but not with role inside this relation. Could you please let me know what is the best way to do it?

非常感谢.

推荐答案

您正在寻找

You are looking for a through=… model [Django-doc]. We can thus create three models, and define a ManyToManyField where the Role is the through model:

class Movie(models.Model):
    title = models.CharField(max_length=45)
    release_date = models.DateField()
    synopsis = models.TextField()
    duration = models.DurationField()

class Person(models.Model):
    first_name = models.CharField(max_length=45)
    last_name = models.CharField(max_length=45)
    birth_date = models.DateField()
    movies = models.ManyToManyField(Movie, through='Role', related_name='actors')

class Role(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
    role = models.CharField(max_length=45)

这篇关于多对多关系-电影-角色-演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆