Taking advantage of model serializers

In Chapter 1Installing the Required Software and Tools, we created the toy model (the Toy class) and its serializer (the ToySerializer class). When we wrote the code for the ToySerializer class, we had to declare many attributes with the same names that we used in the Toy class. The ToySerializer class is a subclass of the rest_framework.serializers.Serializer superclass; it declares attributes that we manually mapped to the appropriate types, and overrides the create and update methods. However, we repeated a lot of code and information that was already included in the toy model, such as the types and the max_length values that specify the maximum length for each string field.

Now, we will take advantage of model serializers to simplify code and to avoid repeating information that is already included in the model. We will create a new version of the existing ToySerializer class that will inherit from the rest_framework.serializers.ModelSerializer superclass instead of inheriting from the rest_framework.serializers.ModelSerializer superclass.

The ModelSerializer class automatically populates a set of default fields and default validators by retrieving metadata from the related model class that we must specify. In addition, the ModelSerializer class provides default implementations for the create and update methods. In this case, we will take advantage of these default implementations because they will be suitable to provide our necessary create and update methods.

Go to the restful01/toys folder and open the serializers.py file. The code file for the sample is included in the hillar_django_restful_04_01 folder, in the restful01/toys/serializers.py file. Replace the code in this file with the following code that declares the new version of the ToySerializer class:

from rest_framework import serializers 
from toys.models import Toy 
 
 
class ToySerializer(serializers.ModelSerializer): 
    class Meta: 
        model = Toy 
        fields = ('id',  
                  'name',  
                  'description', 
                  'release_date', 
                  'toy_category',  
                  'was_included_in_home')

The new version of the ToySerializer class declares a Meta inner class that declares the following two attributes:

  • model: This attribute specifies the model related to the serializer, that is, the Toy class
  • fields: This attribute specifies a tuple of string whose values indicate the field names that we want to include in the serialization from the related model (the Toy class)

The new version of the ToySerializer class doesn't need to override either the create or update methods because the generic behavior provided by the ModelSerializer class will be enough in this case. The ModelSerializer superclass provides implementations for both methods.

With the changes we have made, we removed a nice amount of code from the ToySerializer class. In the new version, we just had to specify the related model and the desired set of fields in a tuple. Now, the types and max_length values related to the toy fields are only included in the Toy class.

If you have previous experience with the Django Web framework, you will realize that the Serializer and ModelSerializer classes in the Django REST framework are similar to the Form and ModelForm classes in Django.

You can press Ctrl + C to quit Django's development server and execute the command that we learned in Chapter 3Creating API Views, to run the server to start it again. In this case, we just edited one file, and in case you didn't stop the development server, Django will detect the changes when we save the changes to the file and it will automatically restart the server.

The following lines show sample output that you will see after you save the changes in the edited Python file. The lines indicate that Django has restarted the development server and successfully performed a system check that identified no issues:

    System check identified no issues (0 silenced).
    October 13, 2017 - 04:11:13
    Django version 1.11.5, using settings 'restful01.settings'
    Starting development server at http://0.0.0.0:8000/
    Quit the server with CONTROL-C. 

You can use the command-line and GUI tools we used in Chapter 3, Creating API Views, to test the new version of our RESTful Web Service that takes advantage of model serializers. The behavior will be the same as in the previous version. However, we definitely have less code to maintain and we have removed duplicated data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset