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 that holds an instance of serializers.SlugRelatedField
with its queryset
argument set to DroneCategory.objects.all()
and its slug_field...