Django RESTful Web Services

Django RESTful Web Services

Title Page
About the Author

Taking advantage of model serializers

In Chapter 1Installing the Required Software and Tools, we created the toy model (the Toy class) and its serializer (the ToySerializer class). When we wrote the code for the ToySerializer class, we had to declare many attributes with the same names that we used in the Toy class. The ToySerializer class is a subclass of the rest_framework.serializers.Serializer superclass; it declares attributes that we manually mapped to the appropriate types, and overrides the create and update methods. However, we repeated a lot of code and information that was already included in the toy model, such as the types and the max_length values that specify the maximum length for each string field.

Now, we will take advantage of model serializers to simplify code and to avoid repeating information that is already included in the model. We will create a new version of the existing ToySerializer class that will inherit from the rest_framework.serializers.ModelSerializer superclass...

Unlock full access

Continue reading with a subscription

Packt gives you instant online access to a library of over 7,500 practical eBooks and videos, constantly updated with the latest in tech

Your notes and bookmarks