contact@digitalnotebooks.co
Stay Organized, Stay Inspired. Let's Write.

10. CRUD using Generics

Back To All Notes
  • Notebook: API Development using Django Framework
  • Speaker: Personal
  • Date Created: March 29, 2025, 10:47 p.m.
  • Owner: Rosilie

Generics according to Django Documentation

Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.

GenericAPIView

This class extends the REST framework's APIView class, adding commonly required behavior for standard list and detail views.

Each of the concrete generic views provided is built by combining  GenericAPIView, with one or more mixin classes.

To read more about Generics, use this link to Medium Digest

The API views are:

ListAPIView RetrieveAPIView
CreateAPIView DestroyAPIView
UpdateAPIView ListAPICreateView
RetrieveUpdateAPIView RetrieveUpdateDestroyAPIView

1. To apply CRUD on Employee model using Generics. Comment out the code using Mixins and add:

 

Running the URL path for listing the employees:

To allow a FORM for creating an employee, we add a generics.CreateAPIView  to inherit.

2. To list the Employee Detail, we use the generics.RetrieveAPIView:

To view the specific record:

3. To update the record, we use the generics.UpdateAPIView:

Updating the form:

After the PUT submission:

4. To delete a record, we add the generics.DestroyAPIView:

 

Before DELETE action:

AFTER deleting record # 3:

5. To delete/update/list a specific record using only generics.RetrieveUpdateDestroyAPIView.

In summary, Generics views are easier and more efficient than using other ways like function-based views or mixins.



No PDF file attached.

Notebook
Notebook Details
Title: API Development using Django Framework
Category: API