Class-based views follow the principles of object-oriented programming. These views are used for reusability and code efficiency.
For this example, we need to create a new app called EMPLOYEES.
1. In the terminal, create a new app:
$ python manage.py startapp employees
2. Register this new app in the SETTINGS.PY
3. Create the new model in MODELS.PY
4. Update the ADMIN.PY
5. Make the migrations. Be sure you are in the correct folder to see the migrations happen.
6. Check the admin panel and create new records for the Employees model:
7. Create a serializer for the Employees model. Go to API\SERIALIZER.PY
8. To retrieve all the records, go to the API\URLS.PY and update:
9. Create the class-based views. Go to the API\VIEWS.PY:
Add the necessary libraries:
Create the class Employee and its methods:
10. To test, use the URL http://127.0.0.1:8000/api/v1/employees/
11.
No PDF file attached.