0

urls.py

from rest_framework import routers
router = routers.DefaultRouter()
router.register('fan/<str:name>', FanView)

urlpatterns = [
   path(r'', include(router.urls)),
]

view.py

class FanView(viewsets.ModelViewSet):
    queryset =  Fan.objects.all()
    serializer_class = FanSerializer

    def get_queryset(self):
    queryset = Fan.objects.all()
    print(self.request.query_params.get('name', None))
    return queryset

Hi i am trying to send name in djnago-rest-framework url. And reading the same in my viewSet.

But, i am always getting None.

I don't wants to send data like fan/?name=foo

Please have a look

Is there any way to achive that ?

1 Answers1

0

What you are trying to access is not in query_params. This is a url parameter and is stored in self.kwargs.lookup_field. You can find here how to access the url parameter.

Al Amin
  • 889
  • 7
  • 12