您将如何建模此数据库关系?

sly*_*ete 2 python database django database-design

我正在建模django中的数据库关系,我想有其他意见.这种关系是一种两对多的关系.例如,患者可以有两名医生:主治医师和初级医生.医生显然有很多病人.

应用程序确实需要知道哪一个是哪个; 此外,存在一个患者的主治医师可以是另一个患者的主要医生的情况.最后,参加和初级通常是相同的.

起初,我正在考虑从患者桌上的两张外键进入医生表.但是,我认为django不允许这样做.此外,在第二个想法,这实际上是很多(两个)到多个关系.

因此,如何在保持与患者相关的医生类型的同时,与django建立这种关系?也许我需要将医生类型存储在多对多关联表中?

谢谢,皮特

mip*_*adi 10

这样的事情怎么样:

class Patient(models.Model):
    primary_physician = models.ForeignKey('Physician', related_name='primary_patients')
    attending_physicial = models.ForeignKey('Physician', related_name='attending_patients')
Run Code Online (Sandbox Code Playgroud)

这允许您有两个外键到同一个模型; 该Physician模型还将具有名为primary_patients和的字段attending_patients.

  • 只需说一下Patient.objects.filter(Q(primary_physician = a_physician)| Q(attending_physicial = a_physician)).我认为这个模型是*最好的方式,即使从原始模式的角度来看也是如此; 它精确而自然地模拟了这种关系.("Q"是1.1的新功能,尚未推出,但我不会接近1.0;它缺少太多东西.我发现Django主干非常稳定,至少目前是这样.) (2认同)