Hello guys so I came to realise that DRF ModelViewSet is a quick way to make rest views, I am trying to use parameters within the router url but the ViewSet does not detect the param
Here is how my viewset looks
class ClientRequests(ModelsViewSet):
serializer_class = serializers.ClientRequestSerializer
def get_queryset(self):
return models.ClientRequest.objects.filter(cliend_id=self.request.kwargs.get('client_id')
now I register the router as below
router = DefaultRouter()
router.register('/<int:client_id/requests', views.ClientRequests, basename='clients request api endpoint')
urlpatterns=[
path('', include(router.urls))
Is this the right way to use restframework router and how can I pass paramters to the url when using router
Routers are used with ViewSets in django rest framework to auto config the urls. Routers provides a simple, quick and consistent way of wiring ViewSet logic to a set of URLs. Router automatically maps the incoming request to proper viewset action based on the request method type(i.e GET, POST, etc).
basename - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one. Note that if the viewset does not include a queryset attribute then you must set basename when registering the viewset.
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as . get() or . post() , and instead provides actions such as . list() and .
Use regex notation
router.register(
r'(?P<client_id>\d+)/requests',
views.ClientRequests,
basename='clients request api endpoint'
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With