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.
Lists, dicts, and JSON objects
From the course: Building RESTful Web APIs with Django
Lists, dicts, and JSON objects
- [Instructor] In order to gather daily, weekly, or monthly product and shopping cart data for our sales report, we need to create a new serializer that uses composite fields. We start by creating the new serializer. It is not a model serializer. It's just a plain serializer, because it's not tied to any specific model. So, it only contains one field. It's a DictField, which has the child parameter set to a ListField, which has its own child parameter set to an IntegerField. So, this is a composite of a composite field. The dictionary keys will be the date as a string. For each value in the dictionary, we're expecting a list of numbers which is the quantity from each shopping cart that the product appeared in. Next, we create a generic API view. We import GenericAPIView. We import the rest_framework_response object. And then, we also import our ProductStatSerializer. Now we can create our stat API view, ProductStats The lookup field is the product id. The serializer class is the…
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)
-
-