了解Django中的关系 [英] Understanding relationships in django

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

问题描述

我是django的新手。我在关系上挣扎。
我有4个应用程序(app1,app2,app3,app4)。 App2、3和4具有相同的字段。这些字段必须在app1中。如何编写模型?请帮忙。
例如,app1是电影(带有标题和位置),app2是音乐(带有标题和位置),而app3是芭蕾舞(带有标题和位置)。 App1必须是电影院,在那里我们可以观看电影,音乐表演和芭蕾舞。

I'm new to django. I'm struggling with relationships. I have 4 apps (app1, app2, app3, app4). App2, 3 and 4 have the same fields. And those fields have to be in app1. How can i write my models? Please help. For example app1 is movies(with title and location), app2 is music(with title and location) and app3 is ballet(with title and location). App1 has to be cinema where we have movies, music performances and ballets.

推荐答案

我不确定您的业务模式,我认为:

I am not sure your business model, I presume:

class Cinema:
    movies = models.ManyToManyField('app1.Movie')
    music = models.ManyToManyField('app2.Music')
    etc...

或者,如果您只想将电影分配给一个电影院:

Alternatively, if you want movies to assign to one cinema only:

class Movie:
    cinema = models.ForeignKey('app1.Cinema', related_name='movies')

这篇关于了解Django中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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