Skip to content

Instantly share code, notes, and snippets.

@shouro
Forked from dkarchmer/drf-nested-views.py
Created January 28, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shouro/eca21091e04e7f6e4f1eebe113fb8da7 to your computer and use it in GitHub Desktop.
Save shouro/eca21091e04e7f6e4f1eebe113fb8da7 to your computer and use it in GitHub Desktop.
Example of a Django Rest Framework ViewSet with nested views
# ViewSets define the view behavior.
class FooViewSet(viewsets.ModelViewSet):
lookup_field = 'slug'
queryset = Foo.objects.all()
serializer_class = FooSerializer
def get_queryset(self):
"""
This view should return a list of all records
"""
return Foo.objects.all().order_by('name')
# GET: /api/v1/foo/1/bar/
@detail_route(methods=['get'])
def bar(self, request, slug=None):
foo = self.get_object()
qs = foo.bars.all().order_by('date')
page = self.paginate_queryset(qs)
if page is not None:
serializer = BarSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = BarSerializer(qs, many=True)
return Response(serializer.data)
# GET: /api/v1/foo/1/foobar/ - THIS FUNCTION HAS NOT BEEN TESTED - MAY NOT WORK
@detail_route(methods=['post'])
def foobar(self, request, slug=None):
foo = self.get_object()
serializer = BarSerializer(data=request.data)
if serializer.is_valid():
new_bar = Bar.objects.create(...)
return Response({'id': new_bar.id})
# POST: /api/v1/foo/new_bar/ with support for multi-payloads
@list_route(methods=['post'])
def new_bar(self, request, *args, **kwargs):
serializer = BarSerializer(data=request.data, many=isinstance(request.data, list))
if serializer.is_valid():
if isinstance(serializer.validated_data, list):
response_status = some_function_to_create_objects_in_batch(serializer.validated_data)
return Response(data, response_status)
else:
response_status = some_function_to_create_objects_in_batch(serializer.validated_data)
return Response(data, status=response_status)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment