Skip to main content

An abstraction layer for creating APIs in Django Rest Framework, supports rpc style APIs.

Project description

Django Api Helper

Overview

The Generic CRUD View in Django is a powerful tool for creating RESTful APIs with Create, Read, Update, and Delete functionalities. This user manual covers how to set up and use the Generic CRUD View, including configuring URLs, permissions, pagination, and views.

Setting Up

pip3 install django-api-helper

Creating a CRUD View

To create a CRUD view for your model, inherit from GenericCRUDView and specify the required attributes. This view is used to create a generic CRUD view for any model. It creates a view with the following endpoints: - GET /api/model_name/ (List) - GET /api/model_name/?pk=1 (Single) - POST /api/model_name/ (Create) - PATCH /api/model_name/?pk=1 (Update) - DELETE /api/model_name/?pk=1 (Delete)

The following attributes must be defined in the child class: - permission_classes - filter_backends - filterset_class - model - pagination_class - serializer_class

Example:

from myapp.views import GenericCRUDView
from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
from myapp.filters import MyModelFilterSet

class MyModelCRUDView(GenericCRUDView):
    model = MyModel
    serializer_class = MyModelSerializer
    filterset_class = MyModelFilterSet
    # Define other attributes like permission_classes, pagination_class

URL Configuration

Set up URL patterns to route requests to your CRUD view.

Example:

from django.urls import path
from myapp.views import MyModelCRUDView

urlpatterns = [
    path('api/mymodel/', MyModelCRUDView.as_view(), name='mymodel-list'),
    # Include other CRUD URLs
]

Permissions

Permissions can be managed at both table and object levels using decorators.

Table-level Permissions:

Use @check_table_permissions('app_name.permission_name') to check if the user has the necessary table-level permission.

Object-level Permissions:

Use @check_object_permissions('app_name.permission_name') for object-level permissions check.

Pagination

Customize pagination by setting the pagination_class attribute in your view.

Example:

from rest_framework.pagination import PageNumberPagination

class CustomPagination(PageNumberPagination):
    page_size = 10
    # Define other pagination settings

# In your CRUD view
class MyModelCRUDView(GenericCRUDView):
    pagination_class = CustomPagination
    # Other attributes...

Using the CRUD View

The CRUD view automatically provides endpoints for listing, retrieving, creating, updating, and deleting objects.

List Objects:

GET /api/model_name/

Retrieve a Single Object:

GET /api/model_name/?pk=1

Create a New Object:

POST /api/model_name/

Update an Object:

PUT /api/model_name/?pk=1

Delete an Object:

DELETE /api/model_name/?pk=1

Advanced Filtering

Leverage django-filter to provide advanced filtering capabilities. Define your filters in filterset_class.

Conclusion

The Generic CRUD View simplifies building RESTful APIs in Django, providing a robust and flexible framework for handling CRUD operations with ease.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_api_helper-0.1.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_api_helper-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file django_api_helper-0.1.0.tar.gz.

File metadata

  • Download URL: django_api_helper-0.1.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_api_helper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ef7b72104885cb22f8c9d42f85023776f9e3a3613c7936850a49d1bdfbe853e
MD5 c54b30361f4167f17b0a0bea0f8efef1
BLAKE2b-256 d0567022d3568b8234098f7122f116e0bfc68eb95f140a67f7d2f9c3bb3d4763

See more details on using hashes here.

File details

Details for the file django_api_helper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_api_helper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ee4d1849375516ad12d543c89ced32c3da6f9ad78791e8d11cff9889001a9bd
MD5 e608a40699486fb0e97459963b6e0df3
BLAKE2b-256 e0ee855beb823dcab6daf7ce4c0b9a80fc854477225982ebb4c4dde2c6b2b56c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page