I'm using Django 3.2 and Django rest framework 3.12.2 and have django.contrib.auth installed. I would like to create an endpoint to create users, so I have set this up in my views ...
class CreateUserView(CreateAPIView):
model = User
permission_classes = [
IsAuthenticated
]
serializer_class = UserSerializer
And I have created this serializer ...
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'password', 'email', 'id', 'first_name', 'last_name')
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if not data[field]:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
return data
def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
email=validated_data['email']
)
return user
Unfortunately when I submit a request withoutcertain fields (e.g. "email"),
read -d '' req << EOF
{
"first_name": "Test 9999",
"last_name": "Dave",
"username": "[email protected]",
"password": "password1"
}
EOF
curl --header "Content-type: application/json" --header "Authorization: Token 6cbf7a80c6dd40e84861c8de143c945aef725676" --data "$req" --request POST "http://localhost:8000/users/"
My validator isn't running and instead I get errors in the "create" part of my serializer ...
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 453, in validate
if not data[field]:
KeyError: 'email'
How do I structure my view such that the validator in my serializer will be recognized?
CodePudding user response:
You can check with:
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if field not in data:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
But likely it makes more sense to let the serializer validate these by making these fields required.