An extra decorator for Django Rest Framework that allows methods of a viewset to accept a nested key.
Project description
# drf-nested-decorator
A decorator that allows view actions to be nested and seeks to be integrated into DRF after proper tests are written.
I didn't try to create a lot of new stuff, i just edited the SimpleRouter and added a decorator
**Use at own risk.** No tests yet! 'Works for me' though.. :)
I've created this functionality because i was in need, and will try to add tests and better organisation to it when i have time.
Want to use it? Star so i know there's someone else besides me that will benefit from my efforts.
## Usage
my_app/views.py
from drf_nested_decorator.decorator import nested_detail_route
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from my_users.permission_classes import ActuallyOwnsCard, IsAwesome
from my_users.serializers import CardSerializer
from my_users.models import Card
class MyUserViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewSet):
authentication_classes = (SessionAuthentication, )
permission_classes = (IsAuthenticated, )
queryset = MyUser.objects.all()
serializer_class = MyUserSerializer
@nested_detail_route(['DELETE'], url_path='cards', permission_classes=(IsAwesome, ActuallyOwnsCard) + permission_classes)
def nested_cards(self, request, pk=None, nested_pk=None)
serializer = CardSerializer(Card.objects.get(pk=nested_pk))
return Response(serializer.data)
urls.py
from drf_nested_decorator.routers import NestedDecoratorSimpleRouter
from my_app.views import MyUserViewSet
router = NestedDecoratorSimpleRouter()
router.register(r'myuser', MyUserViewSet, base_name="myuser")
## To do:
- Add tests, for everything (no decorator tests in DRF found, find or create)
- Test with other packages like drf-nested-routers and drf-extensions
- No formal parameter nested_lookup on decorated method supplied ->> good error message
- Find out from what lower version this is supported, I'm using 3.1.3.
- Automatic lookup of nested model through a nested_queryset?
A decorator that allows view actions to be nested and seeks to be integrated into DRF after proper tests are written.
I didn't try to create a lot of new stuff, i just edited the SimpleRouter and added a decorator
**Use at own risk.** No tests yet! 'Works for me' though.. :)
I've created this functionality because i was in need, and will try to add tests and better organisation to it when i have time.
Want to use it? Star so i know there's someone else besides me that will benefit from my efforts.
## Usage
my_app/views.py
from drf_nested_decorator.decorator import nested_detail_route
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from my_users.permission_classes import ActuallyOwnsCard, IsAwesome
from my_users.serializers import CardSerializer
from my_users.models import Card
class MyUserViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewSet):
authentication_classes = (SessionAuthentication, )
permission_classes = (IsAuthenticated, )
queryset = MyUser.objects.all()
serializer_class = MyUserSerializer
@nested_detail_route(['DELETE'], url_path='cards', permission_classes=(IsAwesome, ActuallyOwnsCard) + permission_classes)
def nested_cards(self, request, pk=None, nested_pk=None)
serializer = CardSerializer(Card.objects.get(pk=nested_pk))
return Response(serializer.data)
urls.py
from drf_nested_decorator.routers import NestedDecoratorSimpleRouter
from my_app.views import MyUserViewSet
router = NestedDecoratorSimpleRouter()
router.register(r'myuser', MyUserViewSet, base_name="myuser")
## To do:
- Add tests, for everything (no decorator tests in DRF found, find or create)
- Test with other packages like drf-nested-routers and drf-extensions
- No formal parameter nested_lookup on decorated method supplied ->> good error message
- Find out from what lower version this is supported, I'm using 3.1.3.
- Automatic lookup of nested model through a nested_queryset?
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file drf_nested_decorator-0.5-py3-none-any.whl
.
File metadata
- Download URL: drf_nested_decorator-0.5-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bb5483fec728f051c69343322b84a69b447ddabc7a40f572e10bb2ce49bfd3d |
|
MD5 | 06024b18932f1a14bf17cdfd37987ebe |
|
BLAKE2b-256 | 539bf8382afd9bbf7e5c092b3d3fec0dc32220ae32329f6c90937285c2dbcdd3 |