0
0
Djangoframework~10 mins

ModelSerializer for model-backed APIs 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 ModelSerializer class from Django REST framework.

Django
from rest_framework.serializers import [1]
Drag options to blanks, or click blank then click option'
ASerializer
BModelSerializer
CAPIView
DViewSet
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Serializer instead of ModelSerializer
Importing from rest_framework.views instead of serializers
2fill in blank
medium

Complete the code to define a serializer class for a Django model named Book.

Django
class BookSerializer([1]):
    class Meta:
        model = Book
        fields = '__all__'
Drag options to blanks, or click blank then click option'
ASerializer
BAPIView
CViewSet
DModelSerializer
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from Serializer instead of ModelSerializer
Forgetting to define the Meta class
3fill in blank
hard

Fix the error in the serializer Meta class to correctly specify the model.

Django
class AuthorSerializer(ModelSerializer):
    class Meta:
        model = [1]
        fields = ['id', 'name', 'email']
Drag options to blanks, or click blank then click option'
AAuthor
Bauthor
CAuthorModel
DAuthors
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names instead of the model class
Using a wrong or undefined model name
4fill in blank
hard

Fill both blanks to create a serializer that only includes the 'title' and 'author' fields from the Book model.

Django
class BookSerializer(ModelSerializer):
    class Meta:
        model = [1]
        fields = [[2]]
Drag options to blanks, or click blank then click option'
ABook
B'title', 'author'
C'id', 'published_date'
DAuthor
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong model name
Not quoting field names or using wrong fields
5fill in blank
hard

Fill all three blanks to create a serializer that excludes the 'created_at' and 'updated_at' fields from the Article model.

Django
class ArticleSerializer(ModelSerializer):
    class Meta:
        model = [1]
        exclude = [[2]]
        fields = [3]
Drag options to blanks, or click blank then click option'
AArticle
B'created_at', 'updated_at'
CNone
D'title', 'content', 'author'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting both fields and exclude with lists (should set fields to None)
Using wrong model name or field names