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

Lists, dicts, and JSON objects

From the course: Building RESTful Web APIs with Django

Start my 1-month free trial

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…

Contents