Skip to main content

Django Easy API - Easy and Fast Django REST framework based on Django-ninja-extra

Project description

PyPI version PyPI version

Test Codecov Downloads

PyPI version PyPI version

Easy CRUD API Framework

  • Zero code for a full CRUD API: Automatic and configurable, inspired by NextJs-Crud.
    • Async CRUD API with Django RBAC security protection
    • Prefetch and retrieve all m2m fields if configured
    • Recursively retrieve all FK/OneToOne fields if configured
    • Excluding fields you do not want, or define a list of sensitive fields of your choice
  • Pure class based Django-Ninja APIs: thanks to Django-Ninja-Extra
  • Domain/Service/Controller Base Structure: for better code organization.
  • Base Permission/Response/Exception Classes: and some handy features to help your API coding easier.
Django-Ninja features:

  Easy: Designed to be easy to use and intuitive.
  FAST execution: Very high performance thanks to Pydantic and async support.
  Fast to code: Type hints and automatic docs lets you focus only on business logic.
  Standards-based: Based on the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
  Django friendly: (obviously) has good integration with the Django core and ORM.

Plus Extra:
  Class Based: Design your APIs in a class based fashion.
  Permissions: Protect endpoint(s) at ease with defined permissions and authorizations at route level or controller level.
  Dependency Injection: Controller classes supports dependency injection with python Injector or django_injector. Giving you the ability to inject API dependable services to APIController class and utilizing them where needed

Install

pip install django-api-framework

Then add "easy" to your django INSTALLED_APPS:

[
    ...,
    "easy",
    ...,
]

Usage

Get all your Django app CRUD APIs up and running in < 1 min

In your Django project next to urls.py create new apis.py file:

from easy.main import EasyAPI

api_admin_v1 = EasyAPI(
    urls_namespace="admin_api",
    version="v1.0.0",
)

# Automatic Admin API generation
api_admin_v1.auto_create_admin_controllers()

Go to urls.py and add the following:

from django.urls import path
from .apis import api_admin_v1

urlpatterns = [
    path("admin/", admin.site.urls),
    path("api_admin/v1/", api_admin_v1.urls),  # <---------- !
]

Now go to http://127.0.0.1:8000/api_admin/v1/docs

You will see the automatic interactive API documentation (provided by Swagger UI). Auto generated APIs List

Boilerplate Django project

A boilerplate Django project for quickly getting started, and get production ready easy-apis with 100% test coverage UP and running: https://github.com/freemindcore/django-easy-api

Auto generated APIs - Users

Thanks to your help

If you find this project useful, please give your stars to support this open-source project. :) Thank you !

Advanced Usage

If CRUD_API_ENABLED_ALL_APPS is set to True (default), all app models CRUD apis will be generated. Apps in the CRUD_API_EXCLUDE_APPS list, will always be excluded.

If CRUD_API_ENABLED_ALL_APPS is set to False, only apps in the CRUD_API_INCLUDE_APPS list will have CRUD apis generated.

Also, configuration is possible for each model, via APIMeta class:

  • generate_crud: whether to create crud api, default to True
  • model_exclude: fields to be excluded in Schema
  • model_fields: fields to be included in Schema, default to "__all__"
  • model_join: prefetch and retrieve all m2m fields, default to False
  • model_recursive: recursively retrieve FK/OneToOne fields, default to False
  • sensitive_fields: fields to be ignored

Example:

class Category(TestBaseModel):
    title = models.CharField(max_length=100)
    status = models.PositiveSmallIntegerField(default=1, null=True)

    class APIMeta:
        generate_crud = True
        model_fields = ["field_1", "field_2",] # if not configured default to "__all__"
        model_join = True
        model_recursive = True
        sensitive_fields = ["password", "sensitive_info"]

Adding CRUD APIs to a specific API Controller

By inheriting CrudAPIController class, CRUD APIs can be added to any API controller. Configuration is available via APIMeta inner class in your Controller, same as the above APIMeta inner class defined in your Django models.

Example:

@api_controller("event_api", permissions=[AdminSitePermission])
class EventAPIController(CrudAPIController):
    def __init__(self, service: EventService):
        super().__init__(service)

    class APIMeta:
        model = Event # django model
        generate_crud = True # whether to create crud api, default to True
        model_fields = ["field_1", "field_2",] # if not configured default to "__all__"
        model_join = True
        model_recursive = True
        sensitive_fields = ["password", "sensitive_info"]

Please check tests/demo_app for more examples.

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_framework-0.2.1.tar.gz (677.6 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_framework-0.2.1-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file django_api_framework-0.2.1.tar.gz.

File metadata

  • Download URL: django_api_framework-0.2.1.tar.gz
  • Upload date:
  • Size: 677.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for django_api_framework-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9d4edf0ebe68c0fff597983e3e5f9be9f5b36668fa8f2798ff73a6584f796eda
MD5 c516c2b49fbd28af8a258994e5fabd75
BLAKE2b-256 205190b4dc302bc410106c983e56db33c9c3edafac0ce6cd9a5dcba900adf41c

See more details on using hashes here.

File details

Details for the file django_api_framework-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_api_framework-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07714625d70b638dbb1fdb960cfeca95dbeb2b0d3a6c0ef8704eb1e2c1e59191
MD5 b0bab0aaf75207b3b606970e0e73b3da
BLAKE2b-256 91b7ab8d95ab79975e53be982f31ef90b058fa57c1984a313e15d686d81dc19d

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