django __init__()获得意外的关键字参数“required”- Rest框架模型序列化程序

edqdpe6u  于 2022-12-05  发布在  Go
关注(0)|答案(2)|浏览(140)

这是我的密码

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

class UserSerializer(serializers.ModelSerializer):
    username = serializers.Field(source="username", required = False)   
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username')

看起来很简单。问题是什么?

cgfeq70w

cgfeq70w1#

将其更改为CharField

username = serializers.CharField(source="username", required = False)
ogq8wdun

ogq8wdun2#

将其更改为CharField并添加allow_blank=True

username = serializers.CharField(source="username", 
                                 required = False,
                                 allow_blank=True)

相关问题