Last active
October 25, 2019 00:11
-
-
Save mckabi/f35afd3221788ca21eb9e51a5c93aa07 to your computer and use it in GitHub Desktop.
DRF router.register(CustomViewSet(parameters, …))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Django REST Framework router + ViewSet + parameters | |
- https://www.django-rest-framework.org/api-guide/routers/ | |
""" | |
class OriginViewSet(ModelViewSet): | |
... | |
@classmethod | |
def as_custom_viewset(cls, **kwargs): | |
class CustomViewSet(cls): | |
queryset = kwargs.get('queryset', None) | |
lookup_field = kwargs.get('lookup_field', 'what') | |
return CustomViewSet | |
router = DefaultRouter() | |
router.register( | |
r'customs', | |
OriginViewSet.as_custom_viewset(queryset=SomeModel.objects.filter(foo=bar)), | |
base_name='customs' | |
) | |
urlpatterns = [path('api/', include(router.urls)), ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment