Skip to main content
 首页 » 编程设计

python之覆盖 save() 方法

2024年10月24日9sxdcgaq8080

我在 Django 中有一个自定义用户模式,用于处理角色或用户类型,创建一个名为 userprofile 的应用程序,它将设置我的自定义用户模型。

在我的 settings.py 中,我有以下配置:

INSTALLED_APPS = [ 
        ... 
    'userprofile', 
] 
#Custom model Users 
AUTH_USER_MODEL = 'userprofile.User' 

我自定义了继承 AbstractUser 类的用户类 (userprofile/models.py),以便根据我的要求向我的用户模型添加一些字段。

我还为具有自己的字段或属性的角色/配置文件用户(MedicalProfile、PatientProfile、PhysiotherapistProfile)创建了这些其他模型

此外,MedicalProfile、PatientProfile、PhysiotherapistProfile 与我的自定义模型/类 User 具有 OneToOneField 关系,因此:

from __future__ import unicode_literals 
from django.conf import settings 
from django.contrib.auth.models import AbstractUser 
from django.db import models 
from django.dispatch import receiver 
from django.db.models.signals import post_save 
 
class User(AbstractUser): 
    is_medical = models.BooleanField(default=False) 
    is_physiotherapist = models.BooleanField(default=False) 
    is_patient = models.BooleanField(default=False) 
    slug = models.SlugField(max_length=100, blank=True) 
    photo = models.ImageField(upload_to='avatars', null = True, blank = True) 
 
    # Overriding the save method 
    def save(self, *args, **kwargs): 
    if self.is_medical: 
        profile = MedicalProfile(user=self) 
        super(User, self).save(self, *args, **kwargs) 
        profile.save() 
 
 
    # We get the profiles user according with their type 
    def get_medical_profile(self): 
        medical_profile = None 
        if hasattr(self, 'medicalprofile'): 
            medical_profile=self.medicalprofile 
        return medical_profile 
 
    def get_patient_profile(self): 
        patient_profile = None 
        if hasattr(self, 'patientprofile'): 
            patient_profile = self.patientprofile 
        return patient_profile 
 
    def get_physiotherapist_profile(self): 
        physiotherapist_profile = None 
        if hasattr(self, 'physiotherapistprofile'): 
            physiotherapist_profile = self.physiotherapistprofile 
        return physiotherapist_profile 
 
    class Meta: 
 
        db_table = 'auth_user' 
 
class MedicalProfile(models.Model): 
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 
    #active = models.BooleanField(default=True) 
    name = models.CharField(max_length=64) 
 
 
class PatientProfile(models.Model): 
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 
    #active = models.BooleanField(default=True) 
    name = models.CharField(max_length=64) 
 
 
class PhysiotherapistProfile(models.Model): 
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 
    #active = models.BooleanField(default=True) 
    name = models.CharField(max_length=64) 

我的问题

我想把我的问题集中在与覆盖过程 save() 方法相关的问题上:

def save(self, *args, **kwargs): 
    if self.is_medical: 
        profile = MedicalProfile(user=self) 
        super(User, self).save(self, *args, **kwargs) 
        profile.save() 

我希望,每创建一个用户,根据他们的字段是否被选中(is_medical、is_patient、is_physiotherapist)自动创建他们的个人资料(MedicalProfile、PatientProfile、PhysiotherapistProfile)

我在覆盖过程中遇到的不便如下:

  • 当我通过 django admin 创建用户时,出现此错误

我不知道,关于将用户 PK 设置为无的原因......

我有什么替代方案可以解决这种情况,当我创建用户时,根据我选择的属性复选框/字段(is_medical、is_physiotherapist、is_patient)保存他们的配置文件实例(MedicalProfile、PhysiotherapistProfile、PatientProfile)?

如果我的问题不适合或不适合 stackoverflow 哲学或我的问题的范围,我在此之前向所有的人道歉。

它的扩展的原因是我想提供所有细节以获得答案

我将不胜感激并感激任何方向

请您参考如下方法:

如果用户不是医疗人员,您需要在保存方法中做一些事情;您仍然需要实际保存对象。

一个固定的实现是:

def save(self, *args, **kwargs): 
    user = super(User, self).save(self, *args, **kwargs) 
    if self.is_medical: 
        MedicalProfile(user=self).save()