Skip to main content

Fast API like decorator for routing DRF Views and Viewsets.

Project description

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,<=3.13

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

@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,))

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

drf-decorator-router-0.2.1.tar.gz (8.2 kB view hashes)

Uploaded Source

Supported by

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