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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can i mention query params in my urls like fan/?name=foo
Yes, but for that, you should remove <str:name> from router.register
OK, how i can define it in urls.py ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.