0
0
Djangoframework~20 mins

ModelSerializer for model-backed APIs in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ModelSerializer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this ModelSerializer validation?

Given the following serializer and input data, what will serializer.is_valid() return?

Django
from rest_framework import serializers
from myapp.models import Product

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ['id', 'name', 'price']

input_data = {'name': 'Book', 'price': -10}
serializer = ProductSerializer(data=input_data)
valid = serializer.is_valid()
AFalse, because price cannot be negative
BTrue, because ModelSerializer does not validate field values by default
CRaises a ValidationError immediately on is_valid() call
DTrue, but serializer.errors will contain the price error
Attempts:
2 left
💡 Hint

Think about how ModelSerializer uses model field validators automatically.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a ModelSerializer for a model with a read-only field?

Choose the correct ModelSerializer code that makes the created_at field read-only.

Django
from rest_framework import serializers
from myapp.models import Event
A
class EventSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ['id', 'name']
        read_only_fields = ['created_at']
B
class EventSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ['id', 'name', 'created_at']
        read_only_fields = ['created_at']
C
class EventSerializer(serializers.ModelSerializer):
    created_at = serializers.ReadOnlyField()
    class Meta:
        model = Event
        fields = ['id', 'name', 'created_at']
D
class EventSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ['id', 'name', 'created_at']
    created_at = serializers.ReadOnlyField()
Attempts:
2 left
💡 Hint

Check how to specify read-only fields in Meta class.

🔧 Debug
advanced
2:00remaining
Why does this ModelSerializer raise a ValidationError on validation?

Consider this serializer and model. Why does calling serializer.is_valid(raise_exception=True) raise a ValidationError?

Django
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email', 'password']

serializer = UserSerializer(data={'username': 'alice', 'email': 'a@example.com'})
serializer.is_valid(raise_exception=True)
serializer.save()
ABecause is_valid() was not called before save()
BBecause serializer.save() requires an explicit commit argument
CBecause 'password' is missing in input data and is required by the model
DBecause the model User does not have an email field
Attempts:
2 left
💡 Hint

Check which fields are required and if they are provided.

state_output
advanced
2:00remaining
What is the value of serializer.validated_data after is_valid()?

Given this serializer and input, what will serializer.validated_data contain after is_valid()?

Django
class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['title', 'content', 'author']

input_data = {'title': 'Hello', 'content': 'World', 'author': 5}
serializer = ArticleSerializer(data=input_data)
serializer.is_valid()
result = serializer.validated_data
AEmpty dict because is_valid() was not called with raise_exception=True
B{'title': 'Hello', 'content': 'World', 'author': <User instance with id=5>}
CRaises ValidationError because author must be a User object
D{'title': 'Hello', 'content': 'World', 'author': 5}
Attempts:
2 left
💡 Hint

Recall the default serializer field used for model ForeignKey fields in ModelSerializer.

🧠 Conceptual
expert
2:00remaining
Which statement about ModelSerializer behavior is true?

Choose the correct statement about how ModelSerializer handles nested writable relationships.

AYou must override <code>create()</code> and <code>update()</code> to handle writable nested serializers
BNested serializers are always read-only in ModelSerializer
CModelSerializer automatically supports writable nested serializers without extra code
DModelSerializer does not support nested serializers at all
Attempts:
2 left
💡 Hint

Think about how nested data is saved in DRF.