gpt4 book ai didi

python - “confirm_password”是此函数的无效关键字参数

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:55 27 4
gpt4 key购买 nike

我使用 DRF 并在 django 上为我的博客编写注册。我在以下行的 views.py 中收到密码错误

User.objects.create_user(**serializer.validated_data)

不知道怎么回事

这是我正在使用的 View

class UserViewSet(ModelViewSet):    
lookup_field = 'username'
queryset = User.objects.all()
serializer_class = UserSerializer

def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(), )
if self.request.method == 'POST':
return (permissions.AllowAny(), )
return (permissions.IsAuthenticated(), IsOwner(), )

def create(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
User.objects.create_user(**serializer.validated_data)
return Response(serializer.validated_data, status=status.HTTP_201_created)
return Response({'status': 'Bad request', 'message': 'Account could not be created with received data'},
status=status.HTTP_400_BAD_REQUEST)

这是随附的序列化器

class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=False)
confirm_password = serializers.CharField(write_only=True, required=False)

class Meta:
model = User
fields = ('id', 'email', 'username', 'first_name', 'last_name', 'password', 'confirm_password',)

def create(self, validation_data):
return User.objects.create(**validation_data)

最佳答案

看看上面的代码,我很确定问题是由于你的用户模型没有 confirm_password 字段,因为 Django 的内置用户模型没有 confirm_password字段。因此,如果您使用的是 django User,请从序列化程序中删除 confirm_password,它将正常工作。

但是如果你想使用你正在使用的方式,你需要使用 AbstractBaseUser 来创建一个自定义的 Django 用户,并且需要你的 models.py 被修改为这。

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models


class AccountManager(BaseUserManager):
def create_user(self, email, password=None, **kwargs):
if not email:
raise ValueError('Users must have a valid email address.')

if not kwargs.get('username'):
raise ValueError('Users must have a valid username.')

account = self.model(
email=self.normalize_email(email), username=kwargs.get('username')
)

account.set_password(password)
account.save()

return account

def create_superuser(self, email, password, **kwargs):
account = self.create_user(email, password, **kwargs)

account.is_admin = True
account.save()

return account


class Account(AbstractBaseUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)

first_name = models.CharField(max_length=40, blank=True)
last_name = models.CharField(max_length=40, blank=True)
tagline = models.CharField(max_length=140, blank=True)

is_admin = models.BooleanField(default=False)

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

objects = AccountManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']

def __unicode__(self):
return self.email

def get_full_name(self):
return ' '.join([self.first_name, self.last_name])

def get_short_name(self):
return self.first_name

您还需要更改您的 settings.py 文件,在底部指定。

AUTH_USER_MODEL = 'authentication.Account'

关于python - “confirm_password”是此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com