Open gsgalloway opened 6 years ago
Another approach is to create a subclass with the suitable get_queryset and make all your viewsets inherit this subclass. For ex:
class TenantModelViewSet(GenericViewSet):
def get_queryset(self):
return TenantModel.objects.all()
class MyModelViewSet1(TenantModelViewSet):
...
class MyModelViewSet2(TenantModelViewSet):
..
Another gotcha is that you'll need to register the route with the extra basename kwarg when using get_queryset():
router = routers.DefaultRouter()
router.register(r'mymodel', MyModelViewSet, basename="mymodel")
To use TenantModel with Django Rest Framework's ViewSets, the following refactor was necessary: