From the course: Building RESTful Web APIs with Django
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Serializer with only selected fields
From the course: Building RESTful Web APIs with Django
Serializer with only selected fields
- In the product serializer, we already have selected some fields for rendering through the serializer. However, we can do a little bit of refactoring to simplify how we added custom field data. We can take the attributes that we set in the two representation method and refactor them using serializer fields. We have a boolean field for is on sale. It is a read-only field. And for the current price, we have a float field which is also read-only. We can now delete two representation and add the fields to metafields so that they appear in the serializer. The read-only serializer field configuration parameter sets whether or not we can write to the field through the serializer. Another example of a serializer field configuration is the source keyword argument. This can be used if you want to rename a field. For instance, if you want the rest API to return a field called 'product name' rather than just 'name'. Back to the code, we can add a description field that overrides the serializer…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Serializer with only selected fields2m 21s
-
Serializer that shows model relationships3m 57s
-
(Locked)
Number fields with serializers2m 59s
-
(Locked)
Date and time fields with serializers3m 4s
-
(Locked)
Lists, dicts, and JSON objects4m 18s
-
(Locked)
Serializer with ImageField and FileField3m 3s
-
(Locked)
-
-