CustomAccounManager.create_user() 缺少 1 个必需的位置参数:'email'
CustomAccounManager.create_user() missing 1 required positional argument: 'email'
我正在尝试在 django 中创建自定义用户模型 - 我将其用作后端。在 django admin 上,我可以毫无问题地创建用户。当我尝试通过邮递员注册一个新帐户时出现了我的问题。
在邮递员中,我在正文中填写了电子邮件字段,但我仍然收到错误 missing 1 required positional argument: 'email'
。
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.utils.translation import gettext_lazy as _
class CustomAccounManager(BaseUserManager):
def create_superuser(self, email, username, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
return self.create_user(self, email, username, password, **other_fields)
def create_user(self, email, username, password, **other_fields):
if not email:
raise ValueError(_('You must provide an email address'))
email = self.normalize_email(email)
user = self.model(email=email, username=username, **other_fields)
user.set_password(password)
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
username = models.CharField(max_length=150, unique = True)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = CustomAccounManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.username
serializers.py
from rest_framework import serializers
from rest_framework.authtoken.models import Token
from .models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'password')
extra_kwargs = {'password': {'write_only': True, 'required': False}}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
Token.objects.create(user=user)
return user
views.py
from rest_framework import viewsets
from django.shortcuts import render
from .models import User
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset =User.objects.all()
serializer_class = UserSerializer
感谢任何帮助!
这是因为您在自定义用户模型中制作 USERNAME_FIELD = 'email'
,所以您必须设置 USERNAME_FIELD = 'username'
并在电子邮件字段中设置
email = models.EmailField(_('email address'), null=True, blank=True)
但如果您需要 USERNAME_FIELD = 'email'
,则必须将其与其他数据一起发送,这取决于您的情况。
我正在尝试在 django 中创建自定义用户模型 - 我将其用作后端。在 django admin 上,我可以毫无问题地创建用户。当我尝试通过邮递员注册一个新帐户时出现了我的问题。
在邮递员中,我在正文中填写了电子邮件字段,但我仍然收到错误 missing 1 required positional argument: 'email'
。
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.utils.translation import gettext_lazy as _
class CustomAccounManager(BaseUserManager):
def create_superuser(self, email, username, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
return self.create_user(self, email, username, password, **other_fields)
def create_user(self, email, username, password, **other_fields):
if not email:
raise ValueError(_('You must provide an email address'))
email = self.normalize_email(email)
user = self.model(email=email, username=username, **other_fields)
user.set_password(password)
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
username = models.CharField(max_length=150, unique = True)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = CustomAccounManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.username
serializers.py
from rest_framework import serializers
from rest_framework.authtoken.models import Token
from .models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'password')
extra_kwargs = {'password': {'write_only': True, 'required': False}}
def create(self, validated_data):
user = User.objects.create_user(**validated_data)
Token.objects.create(user=user)
return user
views.py
from rest_framework import viewsets
from django.shortcuts import render
from .models import User
from .serializers import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset =User.objects.all()
serializer_class = UserSerializer
感谢任何帮助!
这是因为您在自定义用户模型中制作 USERNAME_FIELD = 'email'
,所以您必须设置 USERNAME_FIELD = 'username'
并在电子邮件字段中设置
email = models.EmailField(_('email address'), null=True, blank=True)
但如果您需要 USERNAME_FIELD = 'email'
,则必须将其与其他数据一起发送,这取决于您的情况。