2016-09-23 43 views
0

我目前正在尝试在Django中创建一个健康网络网站。 这个想法是,我的注册应用程序中会有一个名为User的类。存储在用户中的状态之一是用户注册到哪个医院。是否有可能在Django中有一个类作为模型字段?

我在注册应用程序内创建了另一家医院。我希望将该模型医院用作hospital_used状态的模型字段之一。我怎么做?下面是说明关系
UML Diagram

Below is a portion of my UML that illustrates the relationship PNG

这里是我有这么远的代码我的UML的一部分。它用星号封装的代码是我需要帮助的。

class Hospital(models.Model): 
    hospital_Name = models.CharField(max_length=150) 

    def __str__(self): 
     return "Hospital Name: " + str(self.hospital_Name) 


class User(models.Model): 
    PATIENT = 'Pat' 
    DOCTOR = 'Doc' 
    NURSE = 'Nurse' 
    ADMINISTRATOR = 'Admin' 
    user_type_choice = { 
     (PATIENT, 'Patient'), 
     (DOCTOR, 'Doctor'), 
     (NURSE, 'Nurse'), 
     (ADMINISTRATOR, 'Administrator'), 
    } 

    name = models.CharField(max_length=50) 
    dob = models.DateField(auto_now=False) 
    username = models.CharField(max_length=50) 
    *preferred_hospital = Hospital(models.CharField(max_length=50))* 
    patient_type = models.CharField(
     max_length=5, 
     choices=user_type_choice, 
    ) 

谢谢你的StackOverflow伙伴

+0

只需使用外键。 – JRodDynamite

+0

哇,好吧,非常感谢 –

回答

0

我会建议你阅读关于如何创建简单的模型这种材料on tutorials

这里你想要的是使用ForeignKey方法。

name = models.CharField(max_length=50) 
dob = models.DateField(auto_now=False) 
username = models.CharField(max_length=50) 
preferred_hospital = models.ForeignKey(Hospital, on_delete = models.CASCADE) 
patient_type = models.CharField(
    max_length=5, 
    choices=user_type_choice, 
) 

您不必使用on_delete = models.CASCADE但它是你处理,当你删除一个医院应该发生什么最好的。

知道您还可以拥有所有描述为here的OneToOne,ManyToOne或ManyToMany字段。

相关问题