About Lesson
In this Django REST Framework lesson we are going to learn about Django REST Framework Model Serializer, as in the previous lesson we have created our ArticleSerializer, but our ArticleSerializer class is replicating a lot of information that’s also contained in the Article model. It would be nice if we could keep our code a bit more concise. In the same way that Django provides both Form classes and ModelForm classes, REST framework includes both Serializer classes, and ModelSerializer classes.
Let’s look at refactoring our serializer using the ModelSeriaizer class.
1 2 3 4 5 6 7 8 |
from rest_framework import serializers from .models import Article class ArticleSerializer(serializers.ModelSerializer): class Meta: model = Article fields = ['id', 'title', 'description'] |
Now you can see that how easily we have created our ModelSerializer.