Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get my CreateAPIView to recognize the "validate' seciton of my serializer?

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"),

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

read -d '' req << EOF
{
        "first_name": "Test 9999",
        "last_name": "Dave",
        "username": "test3@abc.com",
        "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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading