Configuring serialization and deserialization with relationships

The new RESTful Web Service must be able to serialize the DroneCategory, Drone, Pilot, and Competition instances into JSON representations and vice versa. In this case, we must pay special attention to the relationships between the different models when we create the serializer classes to manage serialization to JSON and deserialization from JSON.

In our last version of the previous RESTful Web Service, we created a subclass of the rest_framework.serializers.ModelSerializer class to make it easier to generate a serializer and reduce boilerplate code. In this case, we will also declare one class that inherits from ModelSerializer. The other three classes will inherit from the rest_framework.serializers.HyperlinkedModelSerializer class.

The HyperlinkedModelSerializer is a type of ModelSerializer that uses hyperlinked relationships instead of primary key relationships, and therefore, it represents the relationships to other model instances with hyperlinks instead of primary key values. In addition, the HyperlinkedModelSerializer generates a field named url with the URL for the resource as its value. As happens with ModelSerializer, the HyperlinkedModelSerializer class provides default implementations for the create and update methods.

Now, go to the restful01/drones folder and create a new Python code file named serializers.py. The following lines show the code that declares the new DroneCategorySerializer class. Notice that we will add more classes to this file later. The code file for the sample is included in the hillar_django_restful_06_01 folder in the restful01/drones/serializers.py file:

from rest_framework import serializers 
from drones.models import DroneCategory 
from drones.models import Drone 
from drones.models import Pilot 
from drones.models import Competition 
import drones.views 
 
 
class DroneCategorySerializer(serializers.HyperlinkedModelSerializer): 
    drones = serializers.HyperlinkedRelatedField( 
        many=True, 
        read_only=True, 
        view_name='drone-detail') 
 
    class Meta: 
        model = DroneCategory 
        fields = ( 
            'url', 
            'pk', 
            'name', 
            'drones') 

The DroneCategorySerializer class is a subclass of the HyperlinkedModelSerializer class. The DroneCategorySerializer class declares a drones attribute that holds an instance of serializers.HyperlinkedRelatedField with many and read_only equal to True. This way, the code defines a one-to-many relationship that is read-only.

The code uses the drones name that we specified as the related_name string value when we created the drone_category field as a models.ForeignKey instance in the Drone model. This way, the drones field will provide us with an array of hyperlinks to each drone that belongs to the drone category.

The view_name value is 'drone-detail' to indicate the browsable API feature to use the drone detail view to render the hyperlink when the user clicks or taps on it. This way, we make it possible for the browsable API to allow us to browse between related models.

The DroneCategorySerializer 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 DroneCategory 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 model related to the serializer, that is, the DroneCategory class. We want to include both the primary key and the URL, and therefore, the code specified both 'pk' and 'url' as members of the tuple. In addition, we want to include the name and the field that provides hyperlinks to each drone that belongs to the drone category. Thus, the code also specifies 'name' and 'drones' as members of the tuple.

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

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

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