Bird
0
0

Why does this serializer raise an error when validating?

medium📝 Debug Q7 of 15
Django - DRF Advanced Features
Why does this serializer raise an error when validating?
class UserSerializer(serializers.Serializer):
    email = serializers.EmailField()

    def validate(self, data):
        if 'example.com' not in data['email']:
            raise serializers.ValidationError('Email must be from example.com domain')
AThe validate method does not return the data
BEmailField does not support domain validation
CValidationError should be raised in validate_email instead
DThe email field is missing required attribute
Step-by-Step Solution
Solution:
  1. Step 1: Check the validate method implementation

    The method raises error correctly but does not return the data dictionary.
  2. Step 2: Understand the requirement of validate method

    It must return the validated data to avoid errors during serializer processing.
  3. Final Answer:

    The validate method does not return the data -> Option A
  4. Quick Check:

    Always return data from validate() method [OK]
Quick Trick: Return data at end of validate() to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Forgetting to return data in validate()
  • Assuming raising error is enough without return
  • Misplacing validation logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes