Using model serializers to eliminate duplicate code

The GameSerializer class we coded in Chapter 5, Developing RESTful APIs with Django 2.1, declares many attributes with the same names that we used in the Game model and repeats information such as the field types and the max_length values. The GameSerializer class is a subclass of the rest_framework.serializers.Serializer superclass and it declares attributes that we manually mapped to the appropriate types, and overrides the create and update methods.

Now we will create a new version of the GameSerializer class that will inherit from the rest_framework.serializers.ModelSerializer superclass. The ModelSerializer class automatically populates both a set of default fields and a set of default validators. In addition, the class provides default implementations for the create and update methods. Hence, this new version of the GameSerializer class will have less code than the previous version and will be easier to maintain.

If you have any experience with Django Web Framework, you will notice that the Serializer and ModelSerializer classes are similar to the Form and ModelForm classes.

Open the serializers.py file in the games_service/games folder. Replace the code in this file with the following lines. The new code declares the new version of the GameSerializer class. The code file for the sample is included in the restful_python_2_06_01 folder, in the Django01/games-service/games/serializers.py file:

from rest_framework import serializers 
from games.models import Game 
 
 
class GameSerializer(serializers.ModelSerializer): 
    class Meta: 
        model = Game 
        fields = ('id',  
                  'name',  
                  'release_date', 
                  'esrb_rating',  
                  'played_once', 
                  'played_times',)

The new GameSerializer class declares a Meta inner class that declares two attributes:

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

There is no need to override either the create or update methods because the generic behavior will be enough in this case. The ModelSerializer superclass provides implementations for both methods.

Now that we've understood how things work with Django REST Framework, we have reduced the boilerplate code that we didn't require in the GameSerializer class. We just needed to specify the desired set of fields in a tuple. Now, the types related to the game fields are included only in the Game class. This way, we are ready to write more serializer classes in a new version of the API that is more complex, without writing a huge amount of code.

Press Ctrl + C to quit Django's development server and execute the following command to start it again:

    python manage.py runserver
..................Content has been hidden....................

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