简体   繁体   English

Django:固定镜头。 模型列表关系,可以在父模型中使用固定数量的OneToOneField而不在子模型中使用FK吗?

[英]Django: For a fixed len. model list relation, is it OK to use a fixed number of OneToOneField's in a parent model rather than a FK in the child model?

I am designing a week-based menu application. 我正在设计一个基于星期的菜单应用程序。 I am wondering if I should just do this: 我想知道是否应该这样做:

class MenuWeek(models.Model):
    sunday = OneToOneField("MenuDay")
    monday = OneToOneField("MenuDay")
    #etc through Saturday

class MenuDay(models.Model):
    # some stuff here, but not a foreign key to MenuWeek

instead of this: 代替这个:

class MenuDay(models.Model):
    week = ForeignKey("MenuWeek") # (with no OneToOne's defined in MenuWeek)

Is the first way a good way to implement a short "fixed-length" list relationship between parent and child or should i just stick with ForeignKey in the MenuDay and enforcing length through forms validation? 第一种方法是在父级和子级之间实现简短的“固定长度”列表关系的好方法吗?还是我应该只在MenuDay中坚持使用ForeignKey并通过表单验证来增强长度? I'm thinking the administration may be cleaner, the first way, where its clear which day of a week you are editing. 我认为管理的第一种方式可能是更清洁的,因为您可以清楚地知道一周中的哪一天在编辑。

Thanks 谢谢

I would never use your first option. 我永远不会使用您的第一选择。 You'll end up repeating yourself all over the place to run a particular section of code for each weekday. 您最终将在每个地方重复一次,以便在每个工作日运行特定的代码部分。 The second option will result in much nicer code down the road. 第二种选择将导致更好的代码。

First, the second option is much better. 首先,第二种选择要好得多。 But you can probably even get rid of MenuWeek and just add a date or week field in MenuDay and do the filtering this way, unless MenuWeek is supposed to hold some data. 但是您甚至可以摆脱MenuWeek而只需在MenuDay添加日期或星期字段,然后以这种方式进行过滤,除非MenuWeek应该保存一些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM