Django扩展用户模型:如何为不同的用户类型实现不同的注册表单?

时间:2019-03-29 19:41:22

标签: django django-models

我正在创建一个约会应用程序,患者可以在此注册约会。目前,我有两种模型,用户,医生和患者。医生和患者模型与用户模型具有一对一的字段。因此,当我创建用户时,他们要么是医生,要么是患者。我遇到的问题是我重新注册了医生,该表格仅要求用户模型中的字段,而不要求医生模型中的字段。请指教。

我已经为两种模型尝试了单独的注册表单。首先创建用户,然后使用其他表单将信息添加到医生模型。但是当我提交医生表单时,它说“用户已经存在”。

models.py

class User(AbstractUser):
    users = (
        ('doctor', 'Doctor'),
        ('patient', 'Patient'),

    user_type = models.CharField(choices=users, max_length=9, default='patient')


class Doctor(models.Model):
    upin = models.AutoField(primary_key=True) #unique physician identification number
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user.user_type = "doctor"
    specialty = models.CharField(max_length=20)
    appointments_per_hour = models.IntegerField(null=True)

class Patient(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user.user_type = "patient"
    pid = models.AutoField(unique=True, primary_key=True) #patient identification

forms.py

class CustomCreationForm(UserCreationForm):
    class Meta(UserCreationForm):
        model = User
        fields = ('first_name','last_name','username','email','user_type',)

class DoctorForm(UserChangeForm):
    class Meta:
        model = Doctor
        fields = ('specialty','appointments_per_hour',)

0 个答案:

没有答案