Creating a Django REST Framework Static ViewSet

Django REST Framework Static ViewSet

A need arose to create an endpoint to hold utility functions, so I created a basic static ViewSet. By doing so, I could still leverage all of the advantages of DRF’s routers, while avoiding creation of a placeholder model.

The snippets below include the ViewSet creation and DRF router binding.

ViewSet code

from rest_framework import viewsets

class UtilityViewSet(viewsets.ViewSet):
    """Static ViewSet which has utility actions"""

    # Set the "queryset" to whichever model permissions should checked against
    # See "Using with views that do not include a queryset"
    queryset = User.objects.none()  # Required for DjangoModelPermissions

    def list(self, request):
        return Response({"description": "Endpoint used to bind general utility functions"})

Router code

from rest_framework.routers import DefaultRouter

router.register("utility", UtilityViewSet, basename="utility")