Skip to content

Instantly share code, notes, and snippets.

Last active November 28, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevin-brown/6112838 to your computer and use it in GitHub Desktop.
Save kevin-brown/6112838 to your computer and use it in GitHub Desktop.

Nested ViewSets for DRF

DRF doesn't allow nested ViewSets natively, so I figured out the least amount of tweaks required to get it working.

The fields that are provided allow you to override the reverse() call with a specific mapping. These cannot be used across urls yet, as it pulls straight from the kwargs.

In your field, you must define map, which is a dictionary of destinaton => previous kwargs keys. These keys are relative to the current URL.

from rest_framework.serializers import HyperlinkedIdentityField, \
class NestedHyperlinkedField(object):
base_obj = None
def __init__(self, *args, **kwargs): = kwargs.pop("map", {})
super(NestedHyperlinkedField, self).__init__(*args, **kwargs)
def field_to_native(self, obj, field_name):
import logging
self.base_obj = obj
return super(NestedHyperlinkedField, self).field_to_native(obj,
def get_url(self, obj, view_name, request, format):
from django.core.urlresolvers import NoReverseMatch
from rest_framework.reverse import reverse
import logging
return super(NestedHyperlinkedField, self).get_url(obj,
view_name, request, format)
except NoReverseMatch:
kwargs = {
serializer = self.parent
view = serializer.context["view"]
for key, kwkey in
base_obj = if self.base_obj else 0
kwargs[key] = view.kwargs.get(kwkey, base_obj)
url = reverse(view_name, kwargs=kwargs, request=request, format=format)
return url
class HyperlinkedIdentityField(NestedHyperlinkedField,
class HyperlinkedRelatedField(NestedHyperlinkedField, HyperlinkedRelatedField):
class ApiRouter(routers.DefaultRouter):
def get_api_root_view(self):
Return a view to use as the API root.
from django.core.urlresolvers import NoReverseMatch
from rest_framework import views
api_root_dict = {}
list_name = self.routes[0].name
for prefix, viewset, basename in self.registry:
api_root_dict[prefix] = list_name.format(basename=basename)
class APIRoot(views.APIView):
_ignore_model_permissions = True
def get(self, request, format=None):
ret = {}
for key, url_name in api_root_dict.items():
ret[key] = reverse(url_name, request=request, format=format)
except NoReverseMatch:
return Response(ret)
return APIRoot.as_view()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment