用户配置文件(客户和员工)的 Django 信号
Django Signals for User Profile (Customer and Employee)
所以我正在尝试创建一个信号来为新创建的用户创建配置文件。但是,我需要两种不同类型的配置文件:客户配置文件和员工配置文件。将创建的配置文件类型在我的用户模型中通过“user_type”字段决定:
user/models.py:
class User(AbstractBaseUser):
USER_TYPES = (
('Employee', 'employee'),
('Customer', 'customer'),
('Vendor', 'vendor')
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
phone_number = models.CharField(max_length=20)
user_type = models.CharField(max_length=8, choices=USER_TYPES)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def __str__(self):
return f'{self.first_name} {self.last_name} : {self.email}'
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
# if user.is_employee == True
class EmployeeProfile(models.Model):
EMPLOYEE_ROLES = (
('Driver', 'driver'),
('Production', 'production'),
('Manager', 'manger')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(max_length=12, choices=EMPLOYEE_ROLES)
def __str__(self):
return str(self.user)
# if user.is_customer == True
class CustomerProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.CharField(max_length=100, null=True)
address = models.CharField(max_length=100, null=True)
address_2 = models.CharField(max_length=100, null=True)
city = models.CharField(max_length=50, null=True)
state = models.CharField(max_length=2, help_text="State Abbreviation (ex: OH)", null=True)
zipcode = models.CharField(max_length=5, null=True)
def __str__(self):
return str(self.user)
然后在我的 user/signals.py 文件中:
@receiver(post_save, sender=User)
def create_customer_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'customer':
CustomerProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def create_employee_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'employee':
EmployeeProfile.objects.create(user=instance)
@receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
def save_profile(sender, instance, created, **kwargs):
user = instance
if created:
if User.user_type == 'customer':
customer_profile = CustomerProfile(user=user)
customer_profile.save()
if created:
if User.user_type == 'employee':
employee_profile = EmployeeProfile(user=user)
employee_profile.save()
当我创建用户时,没有创建配置文件。我能够通过使用一个配置文件模型和标准信号创建一个配置文件:
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
...但理想情况下,我希望根据创建的用户类型有两个单独的配置文件。
在此先致谢。
您可以通过覆盖 User
模型中的 save
方法来做到这一点。
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if self.user_type == 'customer':
CustomerProfile.objects.create(user=self)
elif self.user_type == 'employee':
EmployeeProfile.objects.create(user=self)
所以我正在尝试创建一个信号来为新创建的用户创建配置文件。但是,我需要两种不同类型的配置文件:客户配置文件和员工配置文件。将创建的配置文件类型在我的用户模型中通过“user_type”字段决定:
user/models.py:
class User(AbstractBaseUser):
USER_TYPES = (
('Employee', 'employee'),
('Customer', 'customer'),
('Vendor', 'vendor')
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
phone_number = models.CharField(max_length=20)
user_type = models.CharField(max_length=8, choices=USER_TYPES)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def __str__(self):
return f'{self.first_name} {self.last_name} : {self.email}'
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
# if user.is_employee == True
class EmployeeProfile(models.Model):
EMPLOYEE_ROLES = (
('Driver', 'driver'),
('Production', 'production'),
('Manager', 'manger')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(max_length=12, choices=EMPLOYEE_ROLES)
def __str__(self):
return str(self.user)
# if user.is_customer == True
class CustomerProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.CharField(max_length=100, null=True)
address = models.CharField(max_length=100, null=True)
address_2 = models.CharField(max_length=100, null=True)
city = models.CharField(max_length=50, null=True)
state = models.CharField(max_length=2, help_text="State Abbreviation (ex: OH)", null=True)
zipcode = models.CharField(max_length=5, null=True)
def __str__(self):
return str(self.user)
然后在我的 user/signals.py 文件中:
@receiver(post_save, sender=User)
def create_customer_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'customer':
CustomerProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def create_employee_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'employee':
EmployeeProfile.objects.create(user=instance)
@receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
def save_profile(sender, instance, created, **kwargs):
user = instance
if created:
if User.user_type == 'customer':
customer_profile = CustomerProfile(user=user)
customer_profile.save()
if created:
if User.user_type == 'employee':
employee_profile = EmployeeProfile(user=user)
employee_profile.save()
当我创建用户时,没有创建配置文件。我能够通过使用一个配置文件模型和标准信号创建一个配置文件:
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
...但理想情况下,我希望根据创建的用户类型有两个单独的配置文件。
在此先致谢。
您可以通过覆盖 User
模型中的 save
方法来做到这一点。
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if self.user_type == 'customer':
CustomerProfile.objects.create(user=self)
elif self.user_type == 'employee':
EmployeeProfile.objects.create(user=self)