Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Understanding relationships in django

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...