Discussion:
Specify the parameter of query string as "lookup_field"
k***@gmail.com
2018-05-24 08:34:17 UTC
Permalink
Hi,

I'd like to use the parameter of query string as the lookup_field of
RetrieveAPIView.

As described, it's possible to specify the captured parameter,

$ cat users/urls.py
...
url(r'^user/(?P<user_id>.+)/$', views MyUser.as_view()),
...

$ cat users/views.py
class MyUser(RetrieveAPIView):
serializer_class = UserSerializer

queryset = User.objects.all()
lookup_field = "user_id"

$ curl http://localhost:8000/user/10/
200


However, the following case doesn't work.

$ cat users/urls.py
...
url(r'^user/$', views MyUser2.as_view()),
...

$ cat users/views.py
class MyUser2(RetrieveAPIView):
serializer_class = UserSerializer

queryset = User.objects.all()
lookup_field = "email"

$ curl http://localhost:8000/user/?email='***@example.com'

AssertionError: Expected view MyUser2 to be called with a URL keyword
argument named "email". Fix your URL conf, or set the `.lookup_field`
attribute on the view correctly.
[24/May/2018 16:23:28] "GET /users/ HTTP/1.1" 500 18819


Is there a way to do it?

I read "get_object()" in generics.py. It seems to look up from "kwargs".
https://github.com/encode/django-rest-framework/blob/cdb8a3c3c86bbb74f17f8880c52000602b015ed4/rest_framework/generics.py#L90-L95

Should I use APIView instead of RetrieveAPIView and implement "get()"
method by myself?
Or, can I modify "get_object()" to look up from GET query and send PR?

Thanks,
Teppei
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for 'Specify the parameter of query string as "lookup_field"' (Questions and Answers)
3
replies
Briefly describe the Microsoft's 2000 DNS management?
started 2006-08-17 22:05:37 UTC
computer networking
Loading...