Bird
0
0

Identify the error in this serializer code:

medium📝 Debug Q14 of 15
Django - REST Framework Basics
Identify the error in this serializer code:
class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = 'name', 'price'
AModelSerializer cannot be used with Product model
BMissing import for serializers module
CMeta class should be outside the serializer class
Dfields should be a list or tuple, not separate strings
Step-by-Step Solution
Solution:
  1. Step 1: Check fields attribute syntax

    fields must be a list or tuple, but here it's two separate strings without parentheses or brackets.
  2. Step 2: Confirm correct fields format

    Correct syntax is fields = ['name', 'price'] or fields = ('name', 'price').
  3. Final Answer:

    fields should be a list or tuple, not separate strings -> Option D
  4. Quick Check:

    fields = list/tuple, not comma-separated strings [OK]
Quick Trick: Always wrap multiple fields in list or tuple brackets [OK]
Common Mistakes:
MISTAKES
  • Writing fields as comma-separated strings without brackets
  • Placing Meta class outside serializer
  • Assuming ModelSerializer can't be used with custom models

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes