Defining hyperlinks with serializers.HyperlinkedModelSerializer

Now, add the following code to the serializers.py file to declare the DroneSerializer class. The code file for the sample is included in the hillar_django_restful_06_01 folder in the restful01/drones/serializers.py file:

class DroneSerializer(serializers.HyperlinkedModelSerializer): 
    # Display the category name 
    drone_category = serializers.SlugRelatedField(queryset=DroneCategory.objects.all(), slug_field='name') 
 
    class Meta: 
        model = Drone 
        fields = ( 
            'url', 
            'name', 
            'drone_category', 
            'manufacturing_date', 
            'has_it_competed', 
            'inserted_timestamp') 

The DroneSerializer class is a subclass of the HyperlinkedModelSerializer class. The DroneSerializer class declares a drone_category attribute ...

Get Django RESTful Web Services now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.