0
0
Djangoframework~10 mins

Why advanced DRF features matter 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 main DRF class for creating API views.

Django
from rest_framework.views import [1]
Drag options to blanks, or click blank then click option'
AModelView
BSerializer
CAPIView
DViewSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ViewSet' instead of 'APIView' for basic API views.
Confusing 'Serializer' with view classes.
2fill in blank
medium

Complete the code to add pagination to a DRF view.

Django
from rest_framework.pagination import [1]
Drag options to blanks, or click blank then click option'
ALimitOffsetPagination
BCursorPagination
CBasicPagination
DPageNumberPagination
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'BasicPagination' which does not exist.
Confusing 'LimitOffsetPagination' with page number style.
3fill in blank
hard

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

Django
class UserSerializer(serializers.Serializer):
    email = serializers.[1](max_length=100)
Drag options to blanks, or click blank then click option'
AEmailField
BIntegerField
CCharField
DDateField
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'CharField' which does not validate email format.
Using unrelated field types like 'IntegerField'.
4fill in blank
hard

Fill both blanks to create a viewset with a custom queryset and serializer.

Django
class ProductViewSet(viewsets.[1]):
    queryset = Product.objects.[2]()
Drag options to blanks, or click blank then click option'
AModelViewSet
BReadOnlyModelViewSet
CGenericViewSet
DViewSet
Eall
Ffilter
Gget
Hexclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ReadOnlyModelViewSet' when write operations are needed.
Using 'filter()' without parameters which causes errors.
5fill in blank
hard

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

Django
class AddressSerializer(serializers.Serializer):
    street = serializers.CharField(max_length=100)

class UserSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=50)
    address = [1]()

    def validate_name(self, value):
        if len(value) < [2]:
            raise serializers.ValidationError("Name too short")
        return value

    def validate(self, data):
        if data['name'] == 'admin' and data['address']['street'] == [3]:
            raise serializers.ValidationError("Admin cannot live on this street")
        return data
Drag options to blanks, or click blank then click option'
ANestedSerializer
BCharField
C5
DAddressSerializer
EIntegerField
F10
G"Forbidden Street"
H"Main Street"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'CharField' for the nested address instead of a serializer field.
Setting the minimum length too high or too low.
Using the wrong street name in validation.