我如何在 Django 模型中表示这一点?

发布于 2024-10-01 02:20:53 字数 291 浏览 3 评论 0原文

我无法确定与此之间的关系:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世态炎凉 2024-10-08 02:20:53

令人惊讶的是有这么多不同的答案,我想每个人都有自己的喜好。这就是我的做法:

class Airport(models.Model):
    name = models.CharField(max_length=200)

class Airline(models.Model):
    name = models.CharField(max_length=200)
    terminals = models.ManyToManyField('Terminal', related_name='airlines')

class Terminal(models.Model):
    name = models.CharField(max_length=200)
    airport = models.ForeignKey('Airport', related_name='terminals')

这允许并假设以下条件:

  • 航空公司可以存在于一个或多个
    给定机场有多个航站楼
  • 一个航站楼可以有一个或多个
    航空公司
  • 航站楼只能存在于
    单个机场

请注意,在此设置中,机场和航空公司始终通过航站楼间接连接。我个人认为这是一个功能而不是一个错误。机场始终至少有一个航站楼,即使该航站楼就是整个机场。

或者,

从逻辑的角度来看,您可能认为创建这样的模型稍微更正确:

class Airport(models.Model):
    name = models.CharField(max_length=200)

class Airline(models.Model):
    name = models.CharField(max_length=200)

class Terminal(models.Model):
    name = models.CharField(max_length=200)
    airport = models.ForeignKey('Airport', related_name='terminals')
    airlines = models.ManyToManyField('Airline', related_name='terminals')

实际上,数据的行为大致相同。然而,有人可能会争论说,考虑一个航站楼拥有“航空公司”在概念上比考虑一个特定的航空公司拥有航站楼更有意义。毕竟航空公司不包含航站楼,但每个航站楼都包含这些航空公司。

Surprised that there are so many different answers, I guess people each have their own preferences. This is how I'd do it:

class Airport(models.Model):
    name = models.CharField(max_length=200)

class Airline(models.Model):
    name = models.CharField(max_length=200)
    terminals = models.ManyToManyField('Terminal', related_name='airlines')

class Terminal(models.Model):
    name = models.CharField(max_length=200)
    airport = models.ForeignKey('Airport', related_name='terminals')

This allows for and assumes the following conditions:

  • An airline can be present in one or
    more terminals in a given airport
  • A terminal can have more one or more
    airlines
  • A terminal can only exist in
    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:

class Airport(models.Model):
    name = models.CharField(max_length=200)

class Airline(models.Model):
    name = models.CharField(max_length=200)

class Terminal(models.Model):
    name = models.CharField(max_length=200)
    airport = models.ForeignKey('Airport', related_name='terminals')
    airlines = models.ManyToManyField('Airline', related_name='terminals')

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.

寄人书 2024-10-08 02:20:53
class Airport(models.Model):
    airlines = models.ManyToManyField(Airline, through=Terminal)

class Terminal(models.Model):
    terminal = models.CharField(max_length=200)

class Airline(models.Model):
    terminal = models.ForeignKey(Terminal)
    airport = models.ForeignKey(Airport)
class Airport(models.Model):
    airlines = models.ManyToManyField(Airline, through=Terminal)

class Terminal(models.Model):
    terminal = models.CharField(max_length=200)

class Airline(models.Model):
    terminal = models.ForeignKey(Terminal)
    airport = models.ForeignKey(Airport)
丑疤怪 2024-10-08 02:20:53

机场类(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文