25

Is there any common pattern for routing APIViews (ie, not ViewSets) with Django Rest Framework?

For example, if I've got an API View that takes an optional argument:

class ReportView(APIView):
    def get(self, request, report_name=None):
         # … stuff …
         return Response(report)

Is there any standard pattern — apart from writing out a standard Django URL router — for routing that view?

2 Answers 2

18

Being able to add simple API views is quite useful for displaying the urls in root-api view.

Here is the simplest extension of DefaultRouter that enables registering not only viewsets but also simple API views:

from django.conf.urls import url
from rest_framework import routers, viewsets
from rest_framework.urlpatterns import format_suffix_patterns

class DefaultRouterWithSimpleViews(routers.DefaultRouter):
    """
    Extends functionality of DefaultRouter adding possibility
    to register simple API views, not just Viewsets.
    """

    def get_routes(self, viewset):
        """
        Checks if the viewset is an instance of ViewSet,
        otherwise assumes it's a simple view and does not run
        original `get_routes` code.
        """
        if issubclass(viewset, viewsets.ViewSetMixin):
            return super(DefaultRouterWithSimpleViews, self).get_routes(viewset)

        return []

    def get_urls(self):
        """
        Append non-viewset views to the urls
        generated by the original `get_urls` method.
        """    
        # URLs for simple views
        ret = []
        for prefix, viewset, basename in self.registry:

            # Skip viewsets
            if issubclass(viewset, viewsets.ViewSetMixin):
                continue

            # URL regex
            regex = '{prefix}{trailing_slash}$'.format(
                prefix=prefix,
                trailing_slash=self.trailing_slash
            )

            # The view name has to have suffix "-list" due to specifics
            # of the DefaultRouter implementation.
            ret.append(url(
                regex, viewset.as_view(),
                name='{0}-list'.format(basename)
            ))

        # Format suffixes
        ret = format_suffix_patterns(ret, allowed=['json', 'html'])

        # Prepend URLs for viewsets and return
        return super(DefaultRouterWithSimpleViews, self).get_urls() + ret

Now you can use simple Django views alongside with the rest framework ViewSets:

router = DefaultRouterWithSimpleViews()
router.register(r'users', UserViewSet, 'users')                # <- Viewset!         
router.register(r'reset-pwd', ResetPasswordView, 'reset_pwd')  # <- Simple view!
urlpatterns = router.urls

Update: Added support of format suffixes (thanks to alexander-klimenko)

11
  • Just little notice. This router will produce urls without suffix (*.json, *.jsonp) support. You have to call format_suffix_patterns for extra patterns. Sep 13, 2016 at 10:44
  • 1
    @AlexanderKlimenko thanks! I've updated the answer - please double-check if it's correct. Oct 18, 2016 at 4:52
  • 1
    Maybe to make this example code easier to understand, you could also include a short example of how it would be used?
    – Joakim
    Jan 13, 2017 at 15:05
  • 1
    @М.Б. it might be a typo in your code. Perhaps you missed the first line where router instance is created? Oct 2, 2017 at 16:26
  • 1
    @DavidAvsajanishvili i did it finally! I found a way. I will link git repo here when get project done.
    – М.Б.
    Oct 2, 2017 at 22:56
8

Is there any standard pattern — apart from writing out a standard Django URL router — for routing that view?

ViewSets & Routers are the pattern if you want standardised routing.

If you're using views then just use a regular Django URLconf.

1
  • 31
    How can I add to the browsable API the views that use the regular Django URLconf?
    – ferrangb
    Mar 16, 2015 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.