0
0
Djangoframework~10 mins

Serializers for data conversion in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the serializer base class.

Django
from rest_framework import [1]
Drag options to blanks, or click blank then click option'
Aforms
Bmodels
Cviews
Dserializers
Attempts:
3 left
💡 Hint
Common Mistakes
Importing models instead of serializers
Importing views or forms by mistake
2fill in blank
medium

Complete the serializer class definition to inherit from the correct base class.

Django
class UserSerializer([1].Serializer):
    pass
Drag options to blanks, or click blank then click option'
Amodels
Bserializers
Cviews
Dforms
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from models.Serializer
Inheriting from views.Serializer
3fill in blank
hard

Fix the error in the serializer field declaration.

Django
class ProductSerializer(serializers.Serializer):
    name = serializers.[1]Field(max_length=100)
Drag options to blanks, or click blank then click option'
AChar
BDate
CBoolean
DInteger
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerField for text
Using BooleanField for names
4fill in blank
hard

Fill both blanks to create a serializer that automatically maps model fields.

Django
class BookSerializer(serializers.[1]):
    class Meta:
        model = Book
        fields = [2]
Drag options to blanks, or click blank then click option'
AModelSerializer
BSerializer
C'__all__'
D['title', 'author']
Attempts:
3 left
💡 Hint
Common Mistakes
Using Serializer instead of ModelSerializer
Listing fields manually when __all__ is intended
5fill in blank
hard

Fill all three blanks to define a serializer with custom validation.

Django
class CommentSerializer(serializers.Serializer):
    text = serializers.CharField(max_length=200)

    def validate_[1](self, value):
        if '[2]' in value.lower():
            raise serializers.ValidationError('[3]')
        return value
Drag options to blanks, or click blank then click option'
Atext
Bspam
CNo spam allowed!
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the validation method incorrectly
Checking the wrong field
Not raising ValidationError properly