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.

Number fields with serializers

Number fields with serializers

From the course: Building RESTful Web APIs with Django

Start my 1-month free trial

Number fields with serializers

- [Lecturer] We are now going to use number fields with the product serializer. Let's update the CartItemSerializer first with an IntegerField for the quantity. We're going to ensure that only between one and 100 items of a particular type can be added to the shopping cart. And now for products, we want to ensure that whenever we create or update them through the API, that the minimum price is set to at least $1 and the maximum price is never more than $100,000. We can do this through the FloatField Configuration keyword arguments, min_value and max_value. For example, most e-commerce sites will only allow free items through 100% off coupons and discount codes, such as the Bungie store website for the Destiny game which uses a very large price for products that require a discount code. Now let's implement the FloatField for the product price. We set the min_value to $1 and the max_value to 100,000. The DecimalField is more powerful than the FloatField and actually is in better…

Contents