Bird
0
0

What is the issue with this custom serializer field code?

medium📝 Debug Q6 of 15
Django - DRF Advanced Features
What is the issue with this custom serializer field code?
class MyField(serializers.Field):
    def to_internal_value(self, data):
        return data.upper()
Ato_internal_value should not be overridden in custom fields
BThe to_internal_value method should return a boolean
CThe class must inherit from serializers.Serializer, not serializers.Field
DIt does not validate that 'data' is a string before calling upper()
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the method

    The method calls data.upper(), which only works on strings.
  2. Step 2: Identify missing validation

    There is no check to ensure 'data' is a string, so non-string inputs will cause errors.
  3. Final Answer:

    It does not validate that 'data' is a string before calling upper() -> Option D
  4. Quick Check:

    Always validate input types before string operations [OK]
Quick Trick: Check input type before string methods [OK]
Common Mistakes:
MISTAKES
  • Assuming to_internal_value must return boolean
  • Confusing serializers.Field with serializers.Serializer
  • Believing to_internal_value should never be overridden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes