Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work with DRF 3.5 #275

Closed
oboingo opened this issue Oct 26, 2016 · 12 comments
Closed

Work with DRF 3.5 #275

oboingo opened this issue Oct 26, 2016 · 12 comments

Comments

@oboingo
Copy link

oboingo commented Oct 26, 2016

With the new DRF 3.5, django-rest-auth seems to break and needs something similar to:

"get_queryset()" added to UserDetailViews.

Can we please get an updated version that runs correctly on 3.5?

@maxim-kht
Copy link
Contributor

@oboingo I just tested /rest-auth/user with djangorestframework==3.5.1 and it worked. Please let us know what exact error you are getting.

@oboingo
Copy link
Author

oboingo commented Oct 27, 2016

The error I am getting is:
'UserDetailsView' should either include a queryset attribute, or override the get_queryset() method

@maxim-kht
Copy link
Contributor

maxim-kht commented Oct 27, 2016

UserDetailView only operates on one object, that's why we only override .get_object() but not .get_queryset().

What exactly is calling .get_queryset() ? Is it called during GET, or PUT/PATCH requests? Could you please provide the whole stack trace?

@oboingo
Copy link
Author

oboingo commented Oct 27, 2016

This is while using django-rest-swagger. When I go to the docs page for swagger, it displays the error.

@maxim-kht
Copy link
Contributor

I was able to access it with swagger and also view and update this particular view with the following installed:

djangorestframework==3.5.1
django-rest-swagger==2.0.7

Could you please provide more details?

@jberends
Copy link
Contributor

I have the same problem with GET /rest-auth/user that a queryset is missing in the UserDetailsView.

djangorestframework==3.5.1
django-allauth==0.28.0
django-rest-auth==0.8.1

Traceback (most recent call last):
  File "...//_venv_py34/lib/python3.4/site-packages/channels/handler.py", line 241, in handle_uncaught_exception
    return super(AsgiHandler, self).handle_uncaught_exception(request, resolver, exc_info)
  File ".../_venv_py34/lib/python3.4/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File ".../_venv_py34/lib/python3.4/site-packages/channels/handler.py", line 228, in process_exception_by_middleware
    return super(AsgiHandler, self).process_exception_by_middleware(exception, request)
  File ".../_venv_py34/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File ".../_venv_py34/lib/python3.4/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File ".../_venv_py34/lib/python3.4/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File ".../_venv_py34/lib/python3.4/site-packages/rest_framework/renderers.py", line 701, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File ".../_venv_py34/lib/python3.4/site-packages/rest_framework/renderers.py", line 678, in get_context
    'filter_form': self.get_filter_form(data, view, request),
  File ".../_venv_py34/lib/python3.4/site-packages/rest_framework/renderers.py", line 609, in get_filter_form
    queryset = view.get_queryset()
  File ".../_venv_py34/lib/python3.4/site-packages/rest_framework/generics.py", line 67, in get_queryset
    % self.__class__.__name__
AssertionError: 'UserDetailsView' should either include a `queryset` attribute, or override the `get_queryset()` method.

The code in questions is:
rest_auth/views.py#L124

class UserDetailsView(RetrieveUpdateAPIView):
    """
    Returns User's details in JSON format.
    Accepts the following GET parameters: token
    Accepts the following POST parameters:
        Required: token
        Optional: email, first_name, last_name and UserProfile fields
    Returns the updated UserProfile and/or User object.
    """
    serializer_class = UserDetailsSerializer
    permission_classes = (IsAuthenticated,)

    def get_object(self):
        return self.request.user

IMHO you need to add a queryset attribute (or a def get_queryset(self): in this view, or in a higher view)

from django.contrib.auth import get_user_model

...

class UserDetailsView(RetrieveUpdateAPIView):
    """
    Returns User's details in JSON format.

    Accepts the following GET parameters: token
    Accepts the following POST parameters:
        Required: token
        Optional: email, first_name, last_name and UserProfile fields
    Returns the updated UserProfile and/or User object.
    """
    serializer_class = UserDetailsSerializer
    permission_classes = (IsAuthenticated,)
    # CHANGE IS HERE:
    queryset = get_user_model().objects.all()  # type: User

    def get_object(self):
        return self.request.user

After this fix, all is well in the reponse and no 500 is returned.

@jberends
Copy link
Contributor

jberends commented Nov 7, 2016

Frankly, i have this problem exclusively in firefox (or slimerjs)

headers in FF:

GET http://localhost:8000/rest-auth/user/?_dc=1478526656569
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8000/?no-ui
Cookie: sessionid=f1f4exxxxxxxxxxxxxxxxxxxxxxxxtayr
Connection: keep-alive

In Chrome (chromium, phantomjs)

GET /rest-auth/user/?_dc=1478526866246 HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36
Accept: */*
Referer: http://localhost:8000/?no-ui
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: sessionid=0aexxxxxxxxxxxxxxxxxxxxxxxxxxxxx65is

Will investigate furhter and check it out better.

@jberends
Copy link
Contributor

Due to the differences in the behaviour of XMLHttpRequest generated requests from either Chrome/FF and the fact that the session is already authenticated in FF the problem arose.

Forcing the request to accept json as response ensures a correct response in FF based requests. So use: GET /rest-auth/user?format=json

For me this issue is resolved and more a DRF problem than a rest-auth problem. Although my suggested fix works.

@Akay7
Copy link
Contributor

Akay7 commented Dec 11, 2016

@jberends I'm get that trouble when try to use:

schema_view = get_schema_view(title='Users API', renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])

but with decorators all works well

`@api_view()

@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])

def schema_view(request):`

@maxim-kht maybe lets adding get_queryset to UserDetailsView?

@yunmanger1
Copy link

Monkey patched for now

### MONKEY PATCH till resolved: https://github.com/Tivix/django-rest-auth/issues/275
from rest_auth.views import UserDetailsView
from django.contrib.auth import get_user_model
UserDetailsView.get_queryset = lambda self: get_user_model().objects.none()

@maxim-kht
Copy link
Contributor

maxim-kht commented Dec 22, 2016

I added get_queryset() method to UserDetailsView - should be resolved in latest version.

@rogeliorv-iw
Copy link

As of 17/07/2017 this still requires to manually add get_queryset() to UserDetailsView.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants