Reputation: 110
I am getting an error like this :
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/viewsets.py" in view 87. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch 474. response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in handle_exception 434. self.raise_uncaught_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch 471. response = handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/mixins.py" in list 42. page = self.paginate_queryset(queryset)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/generics.py" in paginate_queryset 172. return self.paginator.paginate_queryset(queryset, self.request, view=self)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/pagination.py" in paginate_queryset 311. self.count = _get_count(queryset)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/pagination.py" in _get_count 54. return len(queryset)
Exception Type: TypeError at /api/userprofiles/ Exception Value: object of type 'NoneType' has no len()
I just want people to get their own profile, when they connect to the api, so instead of applying UserProfile.objects.all
, I thought it would be better if I used UserProfile.objects.get(user=request.user)
.
But as you can well see it isn't working, perhaps because pagination
has some problem cause it is trying to get len()
but the object it's getting is NoneType
although I printed the queryset just after it's fetched so as to determine whether it really is NoneType
, but it isn't.
Here's my UserProfileViewSet :
class UserProfileViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
# queryset = UserProfile.objects.all(user=request.user)
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
pagination_class = LimitTenPagination
def get_queryset(self):
try:
queryset = self.request.user.profile.all() # UserProfile.objects.get(user=self.request.user)
print queryset
except:
queryset = None
return queryset # UserProfile.objects.get(user=self.request.user)
@detail_route(renderer_classes=[renderers.JSONRenderer])
def perform_create(self, serializer):
serializer.save(user=self.request.user)
Upvotes: 1
Views: 1071
Reputation: 110
Well, when I was using UserProfile.objects.get(user=self.request.user)
I was getting an object , instead what I needed to get was a queryset.
So changed the queryset to queryset = UserProfile.objects.filter(user=self.request.user)
Upvotes: 0