我如何在 Django 模型中表示这一点?
我无法确定与此之间的关系:
class Airport(models.Model):
airlines = models.ManyToManyField(Airline)
class Airline(models.Model):
terminal = models.CharField(max_length=200)
问题是每个航空公司都与不同的航站楼相关联,具体取决于请求的机场,因此航站楼不能只是静态文本。
有人知道建模的最佳方法是什么吗?
谢谢
I'm having trouble working out what the relationships should be with this:
class Airport(models.Model):
airlines = models.ManyToManyField(Airline)
class Airline(models.Model):
terminal = models.CharField(max_length=200)
The problem is that each Airline is associated with a different terminal depending on which Airport is requested, so terminal can't just be static text.
Anyone know what the best way to model this is?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
令人惊讶的是有这么多不同的答案,我想每个人都有自己的喜好。这就是我的做法:
这允许并假设以下条件:
给定机场有多个航站楼
航空公司
单个机场
请注意,在此设置中,机场和航空公司始终通过航站楼间接连接。我个人认为这是一个功能而不是一个错误。机场始终至少有一个航站楼,即使该航站楼就是整个机场。
或者,
从逻辑的角度来看,您可能认为创建这样的模型稍微更正确:
实际上,数据的行为大致相同。然而,有人可能会争论说,考虑一个航站楼拥有“航空公司”在概念上比考虑一个特定的航空公司拥有航站楼更有意义。毕竟航空公司不包含航站楼,但每个航站楼都包含这些航空公司。
Surprised that there are so many different answers, I guess people each have their own preferences. This is how I'd do it:
This allows for and assumes the following conditions:
more terminals in a given airport
airlines
a single airport
Note that in this setup airports and airlines are always linked indirectly via Terminals. I think this is a feature rather than a bug, personally. Airports always have at least one terminal, even if the terminal is the entire airport.
Or
You may consider it to be slightly more correct, from a logical point of view, to create the models like so:
In practical terms the data will behave largely the same. One could make an argument, however, that thinking of a terminal having "Airlines" makes more sense conceptually than a given airline having terminals. After all, the airline does not contain the terminals, but each terminal contains these airlines.
机场类(models.Model):
航空公司 = models.ManyToManyField(航空公司)
Terminal = models.CharField(max_length=200)
我认为它可以解决你的问题,只使用一个类
class Airport(models.Model):
airlines = models.ManyToManyField(Airline)
terminal = models.CharField(max_length=200)
i think it would solve your problem use just one class instead