Bird
0
0

What is wrong with this serializer code?

medium📝 Debug Q7 of 15
Django - REST Framework Basics
What is wrong with this serializer code?
class CommentSerializer(serializers.Serializer):
    text = serializers.CharField()

serializer = CommentSerializer(data={'text': 123})
serializer.is_valid()
ARaises a TypeError during serializer creation
BValidation will fail because 'text' expects a string, but got an integer
CValidation will pass because integers are converted to strings automatically
DRaises a ValueError during is_valid() call
Step-by-Step Solution
Solution:
  1. Step 1: Check field type requirements

    CharField expects string input; integer input is invalid.
  2. Step 2: Understand validation behavior

    is_valid() will fail because 123 is not a string, so validation error occurs.
  3. Final Answer:

    Validation will fail because 'text' expects a string, but got an integer -> Option B
  4. Quick Check:

    CharField rejects non-string input [OK]
Quick Trick: CharField requires string input; integers cause validation error [OK]
Common Mistakes:
MISTAKES
  • Assuming automatic type conversion to string
  • Expecting no validation error on wrong type
  • Confusing TypeError with validation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes