Skip to main content

Decorator Router for Django Rest Framework

build

About

Django Rest Framework package to quickly route your views with decorators. Very lightweight and simple code using only Django and Rest Framework as dependencies.

Supported Versions

  • Python 3.6 and above
  • Django >= 2.2
  • Django REST Framework >=3.7.0

Installation

pip install drf-decorator-router

How to Use

main_app/routes.py

from rest_framework import generics, viewsets
from drf_decorator_router import Router

# Declaring the router
router = Router("api/v1", namespace="api-v1")

example_app/views.py

Default Router

@router.route_view("login/", "user-login") # /api/v1/login/
class LoginView(generics.CreateAPIView):
    pass

@router.route_view("company/<int:company_id>/login/", "company-user-login") # /api/v1/company/10/login/
class LoginForCompanyView(generics.CreateAPIView):
    pass

@router.route_viewset("users", "users") # /api/v1/users/
class UserViewSet(viewsets.ModelViewSet):
    pass

Important: The decorated view/viewset must be declared or imported in the views.py file, or else it won't be routed. You can also change the file name from which the views will be loaded by adding a AUTO_ROUTER_MODULES in settings.py. Example: AUTO_ROUTER_MODULES=['decorated_views', 'views'].

main_app/urls.py

from main_app.routers import router

urlpatterns = [
    router.path
]

Reversing

from rest_framework.reverse import reverse

login_view = reverse("api-v1:user-login")
user_list = reverse("api-v1:users-list")
user_detail = reverse("api-v1:users-detail", (10,))

Admin Router

You can turn Django ModelAdmins into secure APIs using the Admin Router

main_app/routes.py

from rest_framework import generics, viewsets
from drf_decorator_router import AdminRouter

# Declaring the router
router = AdminRouter("api/admin", namespace="api-admin")

main_app/admin.py

from django.contrib import admin
from drf_decorator_router.pagination import StandardResultsSetPagination

from . import models
from .routers import admin_router
from .serializers import ProductSerializer


@admin_router.register(models.User, "users")
class UserAdmin(admin.ModelAdmin):
    list_display = ("id", "username", "email") # GET List requests will only show the fields in list_display, while GET Retrieve will show all fields
    list_filter = ("city", "state") # You'll be able to filter in the GET List requests using this filters
    search_fields = ("full_name", "username", "email") # GET List requests will allow a ?s=search_here using the fields in search_fields


# You can customize it even more by passing serializer_cls and other optional fields
@admin_router.register(models.Product, "products", serializer_cls=ProductSerializer)
class ProductAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "price")
    search_fields = ("price",)
    pagination_class = StandardResultsSetPagination

Available Routes

Using the UserAdmin as example, here are the routes available.

GET /api/admin/users/ -> List all users using the list_display fields.
GET /api/admin/users/id/ -> List all fields for specific user.
POST /api/admin/users/ -> Adds new user.
PUT /api/admin/users/id/ -> Updates user using all fields.
PATCH /api/admin/users/id/ -> Updates specific user fields.
DELETE /api/admin/users/id/ -> Deletes a user.

Authentication

By default all the routes provided by the Admin Router requires superuser privilege. The Authorization method is defined by Rest Framework DEFAULT_AUTHENTICATION_CLASSES field.

To Do

  • Custom authentication for routers.
  • Docs page with more examples.

Metadata

Release files for drf-decorator-router 0.3.7

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

Source distribution (sdist)

Source distribution for drf-decorator-router 0.3.7
File Size Uploaded Provenance
drf_decorator_router-0.3.7.tar.gz 12.6 kB Without provenance Details

Release files / drf_decorator_router-0.3.7.tar.gz

Download URL drf_decorator_router-0.3.7.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a720423f13684bd8d278dbed56c77bcbdabb1946641d7a12eab20b04cf6f1399
BLAKE2b-256 checksum
How to use checksums
64b6309419a8adcc04ddf15927f73e6181a9f1d21de89409109d168b6662bfa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.10

Security for drf-decorator-router 0.3.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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