0
0
Djangoframework~10 mins

Serializer validation 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 base serializer class.

Django
from rest_framework import [1]
Drag options to blanks, or click blank then click option'
Aviews
Bserializers
Cmodels
Dfields
Attempts:
3 left
💡 Hint
Common Mistakes
Importing views instead of serializers
Importing models which is unrelated to serializers
2fill in blank
medium

Complete the serializer class to validate that the 'age' field is at least 18.

Django
class UserSerializer(serializers.Serializer):
    age = serializers.IntegerField()

    def validate_age(self, value):
        if value < [1]:
            raise serializers.ValidationError("Must be at least 18 years old.")
        return value
Drag options to blanks, or click blank then click option'
A18
B20
C16
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong age number like 21 or 16
Not raising ValidationError when age is less
3fill in blank
hard

Fix the error in the serializer's validate method to check that 'start_date' is before 'end_date'.

Django
class EventSerializer(serializers.Serializer):
    start_date = serializers.DateField()
    end_date = serializers.DateField()

    def validate(self, data):
        if data['start_date'] > data[[1]]:
            raise serializers.ValidationError("start_date must be before end_date.")
        return data
Drag options to blanks, or click blank then click option'
A'start_date'
B'date'
C'finish_date'
D'end_date'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong key like 'start_date' or 'date'
Using a key that does not exist in the data dictionary
4fill in blank
hard

Fill both blanks to add a custom validation that checks 'email' contains '@' and raise an error if not.

Django
class EmailSerializer(serializers.Serializer):
    email = serializers.CharField()

    def validate_email(self, value):
        if [1] not in value:
            raise serializers.ValidationError("Invalid email address.")
        return [2]
Drag options to blanks, or click blank then click option'
A'@'
Bvalue
Cemail
D'#'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for wrong character like '#'
Returning a wrong variable name instead of value
5fill in blank
hard

Fill all three blanks to create a serializer that validates 'username' length and 'password' confirmation.

Django
class RegisterSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField(write_only=True)
    password2 = serializers.CharField(write_only=True)

    def validate_username(self, value):
        if len(value) < [1]:
            raise serializers.ValidationError("Username too short.")
        return value

    def validate(self, data):
        if data[[2]] != data[[3]]:
            raise serializers.ValidationError("Passwords do not match.")
        return data
Drag options to blanks, or click blank then click option'
A5
B'password'
C'password2'
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using too small a number for username length
Mixing up password field names in validation