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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.