Using Model Serializers
The most common use case for Serializer is to save or retrieve data from Django models. DRF provides ModelSerializer
, which helps us to integrate DRF serializers to a Django model that can serialize/deserialize fields present in it. ModelSerializers
maps between the serializable fields and database fields present in the model definition.
Let’s work with a few basic ModelSerializer
examples to understand the concept. We’ll use the same Blog
Django model we built in Chapter 2 and create BlogSerializer
to serialize the data. We can define our serializers in any place, but to have maintainable code, it is advisable to have an individual serializers.py
file for each app that contains all the Serializer definitions:
# blog/models.py class Blog(BaseTimeStampModel): title = models.CharField(max_length=200) content = models.TextField() author = models.ForeignKey(Author, related_name...