DRF: pass a variable from the router to the ViewSet

56 Views Asked by At

I'm working with Django REST Framework and I want to reuse a ViewSet in two places, but to do that I need to pass some information to the ViewSet class.

This is what my urls.py file currently looks like:

from rest_framework.routers import SimpleRouter

from .views import ContentController

router = SimpleRouter(trailing_slash=False)
router.register("/?", ContentController, basename="content")
urlpatterns = router.urls

But what I'd really like to do is something like this:

router.register("/?", ContentController(type="books"), basename="content")

This way I can reuse the same ContentController in multiple routes, but with a different value for the type variable. But the register method only seems to take a class, not an instance.

Similar questions are all about URL parameters, and that's not related to my question.

0

There are 0 best solutions below