我是Django的新手,目前正在从事培训项目。 问题1-我希望新的注册用户可以收到确认电子邮件。参照https://docs.djangoproject.com/en/2.1/topics/auth/customizing/,我如下创建我的用户模型,并希望在UserManager中的create_user()在新用户创建时被调用,但事实证明从未调用过它。我的实现有误吗?
from authtools.models import AbstractEmailUser
from django.db import models
from authtools.models import UserManager as BaseUserManager
from mysite.models import UserType
from mysite.models import Organisation
class UserManager(BaseUserManager):
def create_superuser(self, **kwargs):
user = self.create_user(**kwargs)
user.is_superuser = True
user.is_staff = True
user.user_type = UserType.objects.get(name='ADMIN')
user.save(using=self._db)
return user
def create_user(self, email, password=None, **kwargs):
user = super().create_user(email, password, kwargs)
user.email_user("Register Confirm", "welcome", "admin@mysite.com")
class User(AbstractEmailUser):
first_name = models.CharField(max_length=255, blank=True)
last_name = models.CharField(max_length=255, blank=True)
user_type = models.ForeignKey(UserType, on_delete=models.CASCADE)
organisation = models.ForeignKey(Organisation, null=True, blank=True, on_delete=models.CASCADE)
modified = models.DateTimeField(auto_now=True)
objects = UserManager()
REQUIRED_FIELDS = ['first_name', 'last_name']
class Meta:
db_table = 'users'
问题2-谁能解释Django框架在触发创建新用户时如何工作?假设收到api调用/ users / add,如何调用create_user()?
答案 0 :(得分:0)
根据Daniel Roseman的说法,该解决方案将覆盖我的用户序列化器中的create()。谢谢!