I am trying to implement filter api in django rest. Something like -
localhost:8000/api/v1/users/?email=abc.xzy@gmail.com/
So it should search for user with the passed filter and return the result.But currently it is returning all user.
URLS.py
url(r'^api/v1/users/$', UserViews.UserList.as_view(), name='userlist_view'),
url(r'^api/v1/users/(?P<email>.+)/$', UserViews.UserList.as_view(), name='userList_view'),
url(r'^api/v1/users/(?P<pk>[0-9]+)/$', UserViews.UserDetail.as_view(), name='userdetail_view'),
UserViews.py
class UserList(generics.ListAPIView):
"""
List all users, or create a new user.
"""
lookup_url_kwarg = "email"
def get(self, request, format=None):
if request.user.is_authenticated():
users = User.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
return Response("User is not authenticated.", status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None):
valid_paylaod, msg = UserListRepository.validations_create_user(request.data)
if not valid_paylaod:
return Response(msg, status=status.HTTP_400_BAD_REQUEST)
result = UserListRepository.create_user_repo(request)
if not result.success:
return Response(str(result.msg), status=result.status )
return Response(UserSerializer(result.data).data, status=result.status)
def get_queryset(self):
# It restricts the userlist by retunning users having emails passed in uery param
user = self.request.user
if user.is_authenticated():
if 'email' in self.request.query_params:
email = self.request.query_params.get('email', None)
users = User.objects.get(email= email)
if not users:
return Response( "User Not found", status=status.HTTP_404_NOT_FOUND)
else:
return Response(UserSerializer(User.objects.all()).data, status.HTTP_200_OK, users)
else:
return Response(UserSerializer().data, status=result.status)
else:
return Response("User is not authenticated.", status=status.HTTP_400_BAD_REQUEST)
Can some one tell why the request is not going to get_queryset() method and going to get() of UserList method. On removing get method, the request goes to get_queryset(self) method. On debugging, I found that I get valid Response before return statement -
(Pdb) UserSerializer(result.data).data
{'parent_id': 2, 'id': 31, 'group_id': '4', 'last_name': 'user', 'email': 'agency22_user@agency22.com', 'organization_id': 0, 'first_name': 'agency22'}
But Still in APi response, I get following error :
Internal Server Error: /api/v1/users/
Traceback (most recent call last):
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
Fi e "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/mixins.py", line 43, in list
if page is not None:
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/serializers.py", line 674, in data
ret = super(ListSerializer, self).data
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/rest_framework/serializers.py", line 239, in data
self._data = self.to_representation(self.instance)
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site- packages/rest_framework/serializers.py", line 614, in to_representation
self.child.to_representation(item) for item in iterable
File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site- packages/django/template/response.py", line 173, in __iter__
raise ContentNotRenderedError('The response content must be '
django.template.response.ContentNotRenderedError: The response content must be rendered before it can be iterated over.
I am not clear why . API URL I am hitting is : localhost:8000/api/v1/users?email=agency22_user@agency22.com