0
0
Djangoframework~10 mins

Why DRF matters for APIs in Django - Test Your Understanding

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

Complete the code to import the Django REST Framework module for serializers.

Django
from rest_framework import [1]
Drag options to blanks, or click blank then click option'
Amodels
Bviews
Cserializers
Dforms
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'views' instead of 'serializers'
Using 'models' which is a Django core module
2fill in blank
medium

Complete the code to create a simple API view using DRF's generic views.

Django
from rest_framework import generics

class BookList(generics.[1]):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
Drag options to blanks, or click blank then click option'
AListAPIView
BCreateAPIView
CRetrieveAPIView
DUpdateAPIView
Attempts:
3 left
💡 Hint
Common Mistakes
Using CreateAPIView which is for creating new objects
Using RetrieveAPIView which is for single object detail
3fill in blank
hard

Fix the error in the serializer by completing the field declaration.

Django
from rest_framework import serializers

class BookSerializer(serializers.Serializer):
    title = serializers.[1](max_length=100)
Drag options to blanks, or click blank then click option'
AIntegerField
BBooleanField
CDateField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerField for text
Using BooleanField which is true/false
4fill in blank
hard

Fill both blanks to create a view that allows listing and creating books.

Django
from rest_framework import generics

class BookListCreate(generics.[1], generics.[2]):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
Drag options to blanks, or click blank then click option'
AListAPIView
BCreateAPIView
CRetrieveAPIView
DUpdateAPIView
Attempts:
3 left
💡 Hint
Common Mistakes
Using RetrieveAPIView which is for single items
Using UpdateAPIView which is for editing existing items
5fill in blank
hard

Fill all three blanks to write a serializer that validates a book's title length and includes an author field.

Django
from rest_framework import serializers

class BookSerializer(serializers.Serializer):
    title = serializers.[1](max_length=100)
    author = serializers.[2](max_length=50)

    def validate_title(self, value):
        if len(value) < [3]:
            raise serializers.ValidationError("Title too short")
        return value
Drag options to blanks, or click blank then click option'
ACharField
C5
DIntegerField
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerField for author
Setting minimum length as a string instead of number