我在Django中有一个自定义用户模式,用于处理角色或用户类型,创建一个名为userprofile的应用程序,该应用程序将设置或将设置我的自定义用户模型。在
在我的设置.py我有以下配置:
INSTALLED_APPS = [
...
'userprofile',
]
#Custom model Users
AUTH_USER_MODEL = 'userprofile.User'
我自定义我的用户类(userprofile/模型.py)继承AbstractUser类以根据我的需求向我的用户模型添加一些字段。在
我还为角色/概要文件用户(MedicalProfile, PatientProfile, PhysiotherapistProfile
)创建了这些具有自己字段或属性的模型
另外,MedicalProfile, PatientProfile, PhysiotherapistProfile
与我的自定义模型/类User
有一个单字段关系,因此:
我的问题
我想把我的问题集中在重写过程save()
方法上:
def save(self, *args, **kwargs):
if self.is_medical:
profile = MedicalProfile(user=self)
super(User, self).save(self, *args, **kwargs)
profile.save()
我想,每个创建用户的用户,自动创建他们的配置文件(MedicalProfile,PatientProfile,Physical TherapistProfile),如果他们的字段被选中(is_medical,is_patient,is_Physical治疗师)
我的不方便之处在于我的覆盖过程如下:
我不知道,关于原因是什么设置用户PK为无。。。在
我可以有什么替代方案来解决这种情况,当我创建一个用户时,根据我选择的属性checkbo/field(is_medical,is_physical therapist,is_patient),保存他们的配置文件实例(MedicalProfile,physical theraprofile,PatientProfile)?在
如果我的问题不符合stackoverflow的哲学或我的问题的范围,我在此之前向您致歉。在
是因为我想把所有的细节都告诉我,以便得到答案
任何介绍我都会很感激和感激的
如果用户不是医务人员,则需要在save方法中执行一些操作;您仍然需要实际保存对象。在
固定的实施方式是:
位于
userprofile/models.py
中的我的class User
正在重写save方法,保持不变:save()方法让我用所有可能的配置文件组合保存用户。在
但是,有更好的方法吗?在
相关问题 更多 >
编程相关推荐