Bird
0
0

Identify the error in this custom serializer field code:

medium📝 Debug Q14 of 15
Django - DRF Advanced Features
Identify the error in this custom serializer field code:
class UpperCaseField(serializers.Field):
    def to_internal_value(self, data):
        return data.upper()

field = UpperCaseField()
print(field.to_internal_value(None))
AField class must inherit from serializers.CharField
Bto_internal_value should return lowercase string
CCalling upper() on None causes an AttributeError
Dto_internal_value method is missing a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Check method call on input

    The code calls data.upper() but data is None, which has no upper() method.
  2. Step 2: Identify error type

    This causes an AttributeError at runtime.
  3. Final Answer:

    Calling upper() on None causes an AttributeError -> Option C
  4. Quick Check:

    None.upper() = AttributeError [OK]
Quick Trick: Check input type before calling string methods [OK]
Common Mistakes:
MISTAKES
  • Assuming None is valid string input
  • Thinking inheritance must be CharField
  • Missing return statement (actually present)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes