英文:
Issue with creating different profiles in Django signals
问题
我在我的Django应用程序中遇到了一个问题,根据user.is_manager的值,我需要创建不同的配置文件,要么是Profile1或Profile2。我正在使用信号来处理这个逻辑。
以下是问题的摘要:
-
我有两个配置文件模型:Profile1和Profile2。Profile1用于管理用户,而Profile2用于非管理用户。
-
在我的managers.py文件中,我在创建用户时将user.is_manager设置为True,以表示管理用户。
-
但是,当我尝试在我的signals.py文件中访问instance.is_manager时,它始终返回False,尽管根据managers.py中设置的值,它应该是True。
在数据库中,is_manager字段被正确设置为True,但在信号处理期间仍然为False
如何解决这个问题的任何建议或见解将不胜感激。
# managers.py
from django.contrib.auth.models import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, phone, username, email, password):
user = self.model(phone=phone, username=username, email=email)
user.set_password(password)
user.save(using=self.db)
return user
def create_superuser(self, phone, username, email, password):
user = self.create_user(phone, username, email, password)
user.is_admin = True
user.is_manager = True # 创建超级用户时也是管理员
user.save(using=self.db)
return
# apps.py
from django.apps import AppConfig
class PanelConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'panel'
def ready(self):
import panel.signals
# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from .models import User
from accounts.models import Profile1, Profile2
@receiver(post_save, sender=User, dispatch_uid="panel.signals.create_profile")
def create_profile(sender, instance, created, **kwargs):
if created:
if instance.is_manager: # 这里返回False
profile = Profile1.objects.create(user=instance)
else:
profile = Profile2.objects.create(user=instance)
profile.save()
我已经审查了我的代码并验证了user.is_manager在managers.py中被正确设置为True。我正在寻求关于为什么instance.is_manager的值在信号中没有准确捕获的指导。
英文:
I'm facing an issue in my Django application where I need to create different profiles, either Profile1 or Profile2, based on the value of user.is_manager. I'm utilizing signals to handle this logic.
Here is a summary of the problem:
-
I have two profile models: Profile1 and Profile2. Profile1 is intended for manager users, while Profile2 is for non-manager users.
-
In my managers.py file, I set user.is_manager to True for manager users during the creation of a user.
-
However, when I try to access instance.is_manager in my signals.py file, it consistently returns False, even though it should be True according to the value set in managers.py.
The is_manager field is correctly set to True in the database but is still False during the signal handling
Any suggestions or insights to resolve this issue would be greatly appreciated.
# managers.py
from django.contrib.auth.models import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, phone, username, email, password):
user = self.model(phone=phone, username=username, email=email)
user.set_password(password)
user.save(using=self.db)
return user
def create_superuser(self, phone, username, email, password):
user = self.create_user(phone, username, email, password)
user.is_admin = True
user.is_manager = True # when create a superuser it's a manager too
user.save(using=self.db)
return
# apps.py
from django.apps import AppConfig
class PanelConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'panel'
def ready(self):
import panel.signals
# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from .models import User
from accounts.models import Profile1, Profile2
@receiver(post_save, sender=User, dispatch_uid="panel.signals.create_profile")
def create_profile(sender, instance, created, **kwargs):
if created:
if instance.is_manager: # this returns False
profile = Profile1.objects.create(user=instance)
else:
profile = Profile2.objects.create(user=instance)
profile.save()
I have already reviewed my code and verified that user.is_manager is correctly set to True in managers.py. I'm looking for guidance on why the value of instance.is_manager is not being captured accurately in the signals.
答案1
得分: 0
类 UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self,用户名, 电子邮件, 电话, 密码, **额外字段):
值 = [电子邮件]
字段_值_映射 = dict(zip(self.model.REQUIRED_FIELDS, 值))
for 字段_名称, 值 in 字段_值_映射.items():
if not 值:
raise ValueError("必须设置 {} 值".format(字段_名称))
电子邮件 = self.normalize_email(电子邮件)
用户 = self.model(电子邮件=电子邮件, 用户名=用户名, 电话=电话, **额外字段)
用户.set_password(密码)
用户.save(using=self._db)
# 创建个人资料...
if 用户.is_manager:
个人资料 = Profile1.objects.create(user=用户)
else:
个人资料 = Profile2.objects.create(user=用户)
return 用户
def create_user(self, 电子邮件, 用户名, 电话, 密码=None, **额外字段):
额外字段.setdefault("is_staff", False)
额外字段.setdefault("is_superuser", False)
return self._create_user(电子邮件, 用户名, 电话, 密码, **额外字段)
def create_superuser(self, 电子邮件, 用户名, 电话, 密码=None, **额外字段):
额外字段.setdefault("is_staff", True)
额外字段.setdefault("is_superuser", True)
if 额外字段.get("is_staff") is not True:
raise ValueError("超级用户必须具有 is_staff=True。")
if 额外字段.get("is_superuser") is not True:
raise ValueError("超级用户必须具有 is_superuser=True。")
return self._create_user(电子邮件, 用户名, 电话, 密码, **额外字段)
英文:
Go with this code no need to signal it's very straight forward...
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self,username, email, phone, password, **extra_fields):
values = [email]
field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))
for field_name, value in field_value_map.items():
if not value:
raise ValueError("The {} value must be set".format(field_name))
email = self.normalize_email(email)
user = self.model(email=email, username=username, phone=phone, **extra_fields)
user.set_password(password)
user.save(using=self._db)
# Profile creating here...
if user.is_manager:
profile = Profile1.objects.create(user=user)
else:
profile = Profile2.objects.create(user=user)
return user
def create_user(self, email, username, phone, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email,username, phone, password, **extra_fields)
def create_superuser(self, email, username, phone, password=None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
return self._create_user(email, username, phone, password, **extra_fields)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论