user()得到一个意外的关键字参数“firstname”

kzmpq1sx  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(406)

你好,我正在创建一个要注册的api,但是我收到了这个错误

User() got an unexpected keyword argument 'firstname'

我尝试了很多方法,但都无法修复,如果有人能帮我修复,我将不胜感激。我的代码:

from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
User = get_user_model()

# User Serializer

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'first_name', 'last_name' ,'username', 'email', 'is_business')

# Register Serializer

class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','first_name' ,'last_name' , 'username', 'brand', 'email', 'is_business', 'password')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'],
                                            firstname=validated_data['first_name'],
                                            lastname=validated_data['last_name'],
                                            brand=validated_data['brand'],
                                            email=validated_data['email'],
                                            password=validated_data['password'])

        return user
w9apscun

w9apscun1#

你打电话给我的是 firstname= 而不是 first_name= 这可能是根本原因
试试像这样的东西

def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'],
                                            first_name=validated_data['first_name'],
                                            last_name=validated_data['last_name'],
                                            brand=validated_data['brand'],
                                            email=validated_data['email'],
                                            password=validated_data['password'])

        return user

相关问题