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.

Creating a CreateAPIView subclass

Creating a CreateAPIView subclass

From the course: Building RESTful Web APIs with Django

Start my 1-month free trial

Creating a CreateAPIView subclass

- [Instructor] Now we want the ability to create new products through the API. In the API views we will create the product creation API view. From REST framework we're going to import the exceptions that we need, and import the create API view. The bottom of the file we're going to create our new product creation, API view. Reusing our serialiser class. And then overwriting the creating method. We're going to extract the price from the parameters. We're going to ensure that the price is set And that the price is above $0, so that it is not free. If either of these conditions fail, we just raise a validation error on the price field, saying must be above $0. We also ... cache the value error exception just in case parsing the price doesn't work. And then we'll erase another validation error, and we say that the price needs to be a number. After that we just call super and create. The validation of the price is to prevent anyone using the REST API from accidentally creating a product…

Contents