2016-05-18 92 views
-2

美好的一天,我得到了以下模型,但我相信有更好的方法來實現我所做的。我的擔心是關於擴展GeneralUser,我並不真的覺得它是一個好主意。 以我現在正在做的方式擴展GeneralUser是否好,還是最好在我想引用它的所有模型中爲GeneralUser創建一個外鍵?如何在django中正確使用多個用戶模型?

class GeneralUser(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    profile_picture = models.ImageField() 
    gender = models.CharField(max_length=1, choices=choices) 
    country = models.CharField(max_length=50) 
    province = models.CharField(max_length=50) 
    city = models.CharField(max_length=50) 
    street = models.CharField(max_length=50) 
    contact_number = models.CharField(max_length=50) 
    post_code = models.IntegerField(max_length=5) 
    date_of_birth = models.DateField() 


class Administrator(GeneralUser): 
    admin_id = models.CharField(max_length=50) 


class Patient(GeneralUser): 
    patient_id = models.CharField(max_length=10) 


class Doctor(GeneralUser): 
    doctor_id = models.CharField(max_length=10) 


class Department(models.Model): 
    doctor_id = models.ForeignKey(Doctor, related_name='doctor') 

class Bill(models.Model): 
    patient_id = models.ForeignKey(Patient, related_name='patient') 


class Appointment(models.Model): 
    patient_id = models.ForeignKey(Patient, related_name='patient') 
    doctor_id = models.ForeignKey(Doctor, related_name='doctor') 

class PatientRecord(models.Model): 
    pass 


class Contact(models.Model): 
    full_name = models.CharField(max_length=80) 
    email = models.EmailField(help_text='[email protected]') 
    contact_number = models.CharField(max_length=14, help_text='+27 074 859 81 93') 
    message = models.TextField(help_text='Please, type in your message') 

任何幫助或想法如何更好地設計表和關係?

+0

爲什麼你不用組來處理? – mtt2p

+0

@ mtt2p不知道如何與羣組 – mkwilfreid

+0

做什麼你的目標是什麼? – mtt2p

回答

1

我認爲那樣適合! (OneToOneField類似於具有unique = True的ForeignKey)

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    force_password_change = models.BooleanField(default=False,editable=False) 

class Distributor(UserProfile): 
    company = models.CharField(max_length=50) 
    street = models.CharField(max_length=60, blank=True, null=True) 
    city = models.CharField(max_length=60, blank=True, null=True) 
    zipcode = models.IntegerField(max_length=10, blank=True, null=True) 
    country = models.CharField(max_length=60, blank=True, null=True) 
    telephone = models.CharField(max_length=15, blank=True, null=True) 
    url = models.CharField(max_length=50, blank=True, null=True) 
    def __unicode__(self): 
     return '%s <%s>' % (self.user.username, self.company) 

class Customer(UserProfile): 
    company = models.CharField(max_length=50) 
    street = models.CharField(max_length=60, blank=True, null=True) 
    city = models.CharField(max_length=60, blank=True, null=True) 
    zipcode = models.IntegerField(max_length=10, blank=True, null=True) 
    country = models.CharField(max_length=60, blank=True, null=True) 
    telephone = models.CharField(max_length=15, blank=True, null=True) 
    url = models.CharField(max_length=50, blank=True, null=True) 
    limit = models.IntegerField(default = 0, blank=True, null=True , editable=False) 
    distributor = models.ForeignKey(Distributor,blank=True,null=True) 
    def __unicode__(self): 
     return ("%s <%s>" % (self.user.username, self.company)) 
+1

這就是要點檢查你的分銷商和客戶,他們有很多共同的領域本來可以更好,如果他們在UserProfile – mkwilfreid

+0

是的你是對的,這使得更有意義 – mtt2p

相關問題