如何在Django中定义3个以上模型之间的多对多关系?

时间:2022-10-05 14:21:37

Right now I have these models: Movie, MovieFormat (eg, dvd, bluray, etc), Region, and I'd like to make a relationship between these to store (Movie, MovieFormat, Region, Date) tuples (movie releases)

现在我有这些模型:电影,MovieFormat(例如,DVD,蓝光等),Region,我想在这些模式之间建立关系(Movie,MovieFormat,Region,Date)元组(电影发行)

I could just put a many-to-many field on Movie referencing Region through another model that has format and date fields, but that doesn't seem like the 'right' way to do this.

我可以通过另一个具有格式和日期字段的模型在Movie引用Region上放置一个多对多字段,但这似乎不是“正确”的方法。

1 个解决方案

#1


1  

I don't understand completely what you are trying to do, but i think you are seeing this in the wrong way.

我完全不明白你想要做什么,但我认为你是以错误的方式看到这一点。

With the ideas i have in mind about what is a Movie (i'm trying to think object oriented here), what you should use is something like this:

我想到的是关于什么是电影的想法(我试图在这里思考面向对象),你应该使用的是这样的:

class Movie(models.Model):
    format = models.ForeignKey(MovieFormat)
    region = models.ForeignKey(Region)
    date = models.Datefield()
    ... other fields you want, like title for instance

With a model like this, you can store in your database (stop thinking in objects now) movies with different formats (formats are stored in other table so you have your database normalized), different region and dates for each movie (same thing with region as with format here).

使用这样的模型,您可以存储在您的数据库中(现在停止在对象中思考)具有不同格式的电影(格式存储在其他表中,以便您对数据库进行规范化),每个电影的不同区域和日期(与区域相同)与格式一样)。

Is this what you want? If no, try to be more specific with your question... Add an example with some movies and their relations with regions and formats...

这是你想要的吗?如果不是,请尝试更具体地解决您的问题...添加一些电影及其与地区和格式的关系的示例...

EDIT :

Now i see what you want. I don't know exactly how to name the model i'm going to add, but i'm sure you are going to find an appropiate name:

现在我明白了你想要什么。我不确切知道如何命名我要添加的模型,但我相信你会找到一个合适的名字:

class Movie(models.Model):
    ... fields that you want for a movie, like foreing key to a producer for instance

class MovieEdition(models.Model):
    movie = models.ForeignKey(Movie)
    format = models.ForeignKey(MovieFormat)
    region = models.ForeignKey(Region)
    date = models.Datefield()
    ... other fields, like movie title for that region (the related region)

Hope it helps! Cheers!

希望能帮助到你!干杯!

#1


1  

I don't understand completely what you are trying to do, but i think you are seeing this in the wrong way.

我完全不明白你想要做什么,但我认为你是以错误的方式看到这一点。

With the ideas i have in mind about what is a Movie (i'm trying to think object oriented here), what you should use is something like this:

我想到的是关于什么是电影的想法(我试图在这里思考面向对象),你应该使用的是这样的:

class Movie(models.Model):
    format = models.ForeignKey(MovieFormat)
    region = models.ForeignKey(Region)
    date = models.Datefield()
    ... other fields you want, like title for instance

With a model like this, you can store in your database (stop thinking in objects now) movies with different formats (formats are stored in other table so you have your database normalized), different region and dates for each movie (same thing with region as with format here).

使用这样的模型,您可以存储在您的数据库中(现在停止在对象中思考)具有不同格式的电影(格式存储在其他表中,以便您对数据库进行规范化),每个电影的不同区域和日期(与区域相同)与格式一样)。

Is this what you want? If no, try to be more specific with your question... Add an example with some movies and their relations with regions and formats...

这是你想要的吗?如果不是,请尝试更具体地解决您的问题...添加一些电影及其与地区和格式的关系的示例...

EDIT :

Now i see what you want. I don't know exactly how to name the model i'm going to add, but i'm sure you are going to find an appropiate name:

现在我明白了你想要什么。我不确切知道如何命名我要添加的模型,但我相信你会找到一个合适的名字:

class Movie(models.Model):
    ... fields that you want for a movie, like foreing key to a producer for instance

class MovieEdition(models.Model):
    movie = models.ForeignKey(Movie)
    format = models.ForeignKey(MovieFormat)
    region = models.ForeignKey(Region)
    date = models.Datefield()
    ... other fields, like movie title for that region (the related region)

Hope it helps! Cheers!

希望能帮助到你!干杯!