简体   繁体   中英

django - BaseSerializer.save() takes 1 positional argument but 2 were given

The following error occurred while implementing signup.. When it is a get request, the value of the field appears well, but when i send a post request, the following error occurs.

[ BaseSerializer.save() takes 1 positional argument but 2 were given ]

What should I do? Help me please.. ㅠ_ㅠ

<serializers.py>

from .models import User
from dj_rest_auth.registration.serializers import RegisterSerializer
from rest_framework import serializers


class SignUpSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['email', 'password', 'realname', 'nickname', 'address', 'phone', 'height', 'weight']

    def save(self, request):
        user = super().save(request)
        realname = self.data.get('realname')
        nickname = self.data.get('nickname')
        address = self.data.get('nickname')
        phone = self.data.get('phone')
        height = self.data.get('height')
        weight = self.data.get('weight')
        if realname:
            user.realname = realname
        if nickname:
            user.nickname = nickname
        if address:
            user.address = address
        if phone:
            user.phone = phone
        if height:
            user.height = height
        if weight:
            user.weight = weight
        user.save()
        return user

<models.py>

class User(AbstractUser):
    username = None
    email = models.EmailField(max_length=255, unique=True)
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    
    objects = UserManager()

    realname = models.CharField(max_length=50, blank=True)
    nickname = models.CharField(max_length=50, blank=True)
    address = models.CharField(max_length=200, blank=True)
    phone = models.CharField(max_length=100, blank=True)
    height = models.DecimalField(max_digits=4, decimal_places=1, default=0)
    weight = models.DecimalField(max_digits=4, decimal_places=1, default=0)

    def __str__(self):
            return self.email

<setting.py>

    REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'accounts.serializers.SignUpSerializer'
}

You must be using django-rest-auth , and you want to customize the register serializer.

I think the parent class of the SignUpSerialier is wrong. It should be rest_auth.registration.serializers.RegisterSerializer , not ModelSerializer . It's not a model serializer, so you need to set the additional fields manually.

You should be careful to specify the type correctly according to the field type of the User model. ( serializers.CharField, serializers.IntegerField, etc )

By deriving from the RegisterSerializer , you can redefine the save(self, request) method.

class SignUpSerializer(RegisterSerializer):
    realname = serializers.CharField(required = True)
    nickname = ...
    address = ...
    phone = ...
    height = ...
    weight = ...

    def save(self, request):
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM