Bird
0
0

What is wrong with this DRF serializer code?

medium📝 Debug Q14 of 15
Django - REST Framework Basics
What is wrong with this DRF serializer code?
from rest_framework import serializers

class UserSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=100)
    email = serializers.EmailField()

    def create(self, validated_data):
        return User.objects.create(validated_data)
ASerializer classes cannot define create methods.
BThe create method should unpack validated_data with ** before passing to create().
CCharField does not accept max_length argument.
DEmailField is not a valid serializer field.
Step-by-Step Solution
Solution:
  1. Step 1: Review create method usage

    The create method must pass validated_data as keyword arguments using ** to User.objects.create().
  2. Step 2: Check other parts for errors

    EmailField and CharField usage are correct; serializers can define create methods.
  3. Final Answer:

    The create method should unpack validated_data with ** before passing to create(). -> Option B
  4. Quick Check:

    Use **validated_data in create() = D [OK]
Quick Trick: Use ** to unpack validated_data in create() [OK]
Common Mistakes:
MISTAKES
  • Passing dict directly without unpacking
  • Thinking EmailField is invalid
  • Believing serializers can't have create methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes