Fast API like decorator for routing DRF Views and Viewsets.
Project description
Decorator Router for Django Rest Framework
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for drf-decorator-router-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 539ff6d2649837918f46a8fc1353ca4efa491eec9a6db6d0afd8d39d1ffb93f1 |
|
MD5 | a378c4fe569061035fa9802575e5a9e2 |
|
BLAKE2b-256 | a35c24d751313ea44ca0d66252134436f09a7f84f623e911b1029ada3e7c2e1c |