如何在django中正确使用多个用户模型?

时间:2016-05-18 15:14:29

标签: django database-design django-models

美好的一天,我得到了以下型号,但我相信有更好的方法来实现我所做的。我担心的是扩展GeneralUser,我并不是真的觉得这是一个好主意。 如我现在所做的那样扩展GeneralUser是不错的,还是在我想引用它的所有模型中为GeneralUser创建一个外键更好?

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='name@domain.com')
    contact_number = models.CharField(max_length=14, help_text='+27 074 859 81 93')
    message = models.TextField(help_text='Please, type in your message')

有关如何更好地设计表格和关系的任何帮助或想法?

1 个答案:

答案 0 :(得分:1)

我认为这样适合! (OneToOneField类似于具有唯一= 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))