Skip to main content

django-ninja-fsbr

File-System Based Routing for django-ninja

Why

  • To enforce an opinionated project structure, that does not make me think.
  • To enable easily mapping routes/endpoints to files.
  • For fun. :)

Usage

The usage is most easily explained using an example. Let's say you want to have the following API:

/products/ GET, POST
/products/<product_id>/ GET
/products/<product_id>/stock/ GET, PUT

You can achieve this using the following directory layout:

├── root.py
└── views
    └── products
        ├── index.py
        ├── __init__.py
        └── product_id
            ├── index.py
            ├── __init__.py
            └── stock.py

The root module contains the definition of a router which specifies the base directory, that your automatic routes will be relative to:

from ninja_fsbr import FilesystemBasedRouter

api = NinjaApi(
    ...
)

router = FilesystemBasedRouter(views_module="root.views")
router.auto_discover()

api.add_router("api/", router)

To define an "auto" route, just use the @router.auto_route decorator instead django-ninja's @router.<method> decorators:

from root import router

@router.auto_route(
  methods=["GET"],  # optional, if method name starts with get_ (or any other HTTP method name)
  ... # other django-ninja arguments here
)
def get_stock(request, product_id):
  ...

Conventions

  • Any directory with the ending _id will be treated as a path parameter in the generated route.
  • The index.py will resolve to the path itself.
  • The supported HTTP method can be set by either passing a methods argument to auto_route or by prefixing the view function with the verbs i.e. get_put_stock (to support both GET and PUT requests)
  • Routes are sorted by name

Metadata

Release files for django-ninja-fsbr 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-ninja-fsbr 0.2.1
File Size Uploaded
django_ninja_fsbr-0.2.1.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ninja-fsbr 0.2.1
File Interpreter ABI Platform
django_ninja_fsbr-0.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 10.4 kB

Release files / django_ninja_fsbr-0.2.1.tar.gz

Download URL django_ninja_fsbr-0.2.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
77963f3d32f6ceee2e84ae836394faebd474fc5507a8cf39f5935d7b9834f3bd
BLAKE2b-256 checksum
How to use checksums
8d3b1fef0144fa69d258e2c66ecbe2dba08e4af5fa4f2e965d7a1f2c4417f8c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / django_ninja_fsbr-0.2.1-py2.py3-none-any.whl

Download URL django_ninja_fsbr-0.2.1-py2.py3-none-any.whl
Size 3.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
96e1aafaae23c54a4e76462787d24d7be50bc054e6e7b2d8322f8463c949d8df
BLAKE2b-256 checksum
How to use checksums
adbba927b9fd5fad0cefb90fcfd5689e951402ac6a5226798812a7cb6d902589
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page