A validator can be added to any field in a Django REST Framework serializer. Learn how to create a number field that accepts integers, floats, and decimal numbers constrained to minimum and maximum values by exploring validators, the number validator and custom validators for 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 …
- Creating a serializer
- Working with API views
- Filtering back ends
- Enabling pagination
- Executing CRUD operations
- Managing serializer fields
- Testing API views
Skill Level Intermediate
Deploying Django Apps: Make Your Site Go Livewith Nick Walter2h 43m Intermediate
Demo project overview1m 19s
1. Serializing, Listing, Filtering, and Paginating Models
2. Create, Retrieve, Update, and Delete (CRUD) Operations for Models
3. Managing Serializer Fields, Relations, and Validation
4. Testing API Views
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.