【问题标题】:How to connect one django model to many other models?如何将一个 django 模型连接到许多其他模型?
【发布时间】:2023-03-19 23:52:01
【问题描述】:

我正在尝试开发一个物业管理系统。我已经创建了卧室和浴室模型。在每间卧室和浴室中,我需要添加照明、气候和娱乐模型。我已经创建了这些模型,但我不确定是否应该创建另一个模型,如“Lighting_Bedroom”并使用指向这两个模型的 ForeignKeys,或者是否有其他方法。

// Bedroom Model
class Bedroom(models.Model):
    type = models.CharField(db_column='Type', choices=BEDROOM_TYPE_CHOICES, max_length=50)
    bed_dimensions = models.CharField(db_column='Bed_Dimension', choices=BED_DIMENSION_CHOICES, max_length=30)
    image = models.ImageField(null=True)
    ensuite = models.BooleanField(default=False)
    notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True)  # Field name made lowercase.
    property = models.ForeignKey(Property, null=False, on_delete=models.CASCADE)

// Bathroom Model
class Bathroom(models.Model):
    sink = models.CharField(db_column='Sink', choices=SINK_BATHROOM_CHOICES, max_length=50)
    shower = models.CharField(db_column='Shower', choices=SHOWER_BATHROOM_CHOICES, max_length=50)
    tower_rail = models.CharField(db_column='Tower_Rail', choices=TOWER_RAIL_BATHROOM_CHOICES, max_length=50)
    image = models.ImageField(null=True)
    toilet = models.BooleanField(default=False)
    bidet = models.BooleanField(default=False)
    bath = models.BooleanField(default=False)
    extractor_fan = models.BooleanField(default=False)
    notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True)  # Field name made lowercase.
    property = models.ForeignKey(Property, null=False, on_delete=models.CASCADE)

// Properties

class Lighting(models.Model):
    type = models.CharField(db_column='Type', choices=LIGHTING_TYPE_CHOICES, max_length=30)
    bulb_type = models.CharField(db_column='Bulb_Type',  max_length=30)

class Climatization(models.Model):
    type = models.CharField(db_column='Type', choices=CLIMATIZATION_TYPE_CHOICES, max_length=30)
    use = models.CharField(db_column='Use', choices=CLIMATIZATION_USE_CHOICES, max_length=30)
    brand = models.CharField(db_column='Brand',  max_length=30)
    model = models.CharField(db_column='Model',  max_length=50)
    remote_control = models.BooleanField(default=False)
    notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True)  # Field name made lowercase.

到目前为止,我还没有创建第三个模型来连接两个模型,因为我已经阅读了有关 ManyToManyField 的内容,但我不确定这是否是我正在寻找的。​​p>

【问题讨论】:

    标签: python django database django-models


    【解决方案1】:

    您可以选择在每个房间类型中使用OneToOneField 的RoomFitting 模型,然后您的Lighting 模型可以具有该模型的外键。

    class Bedroom(models.Model):
        fittings = OneToOneField(RoomFitting)
    
    class Lighting(models.Model):
        fitting = ForeignKey(RoomFitting)
    

    这完全取决于该类的使用方式和预期使用方式,例如,并非所有照明都适合在浴室内使用

    【讨论】:

    • 我从你的评论中想到了一个想法,你能看看我发布的答案吗?谢谢!!
    • @LuisSilva - 你的回答与你的问题无关
    • 我已经删了,到时候再想办法
    • 发现这篇文章几乎就是我要找的:stackoverflow.com/questions/11122597/…
    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 2021-10-10
    • 2015-03-03
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多