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
UserWarning: X is not compatible with schema generation: due to AnonymousUser #966
Comments
Not much we can do here. Any method needing to call Either way the issue is better addressed as part of DRF's introspection logic. (But there's not a simple answer there yet either.) |
One possible workaround might be to define something like this in the
|
Thanks @gunthercox your comment really helped me out. It put me on the path to the solution, but ultimately what worked for me was this:
Hope this helps someone else in a similar situation. |
I think that the suggested approach is this one: def get_queryset(self):
if getattr(self, "swagger_fake_view", False):
return MyModel.objects.none()
# ... the rest of the method code ... I've seen that in this thread: axnsan12/drf-yasg#333 |
The ViewSets were not compatible with schema generation. django-filter raised UserWarnings. See carltongibson/django-filter#966
The ViewSets were not compatible with schema generation. django-filter raised UserWarnings. See carltongibson/django-filter#966
|
I wrote a decorator so DRY principle is followed when using this fix: from django.db.models import QuerySet
def django_filter_warning(get_queryset_func):
"""
This decorator is used to fix a warning in django-filter.
See: https://github.com/carltongibson/django-filter/issues/966
"""
def get_queryset(self):
if getattr(self, "swagger_fake_view", False):
return QuerySet()
return get_queryset_func(self)
return get_queryset
class MyCoolView:
...
@django_filter_warning
def get_queryset(self):
"""
Returns reports that belong to the current user.
"""
return self.request.user.reports.all() |
I am looking at the UserWarnings I've tried to enhance in #903 again:
Those happen mostly because an AnonymousUser is used, which is not compatible with most of our view's
get_queryset()
methods, which assume that the user is authenticated (and has certain methods in particular).I am using drf_yasg here.
I wonder if there is a way to improve this, e.g. by using an authenticated user for it (would be a drf-yasg issue then probably).
Traceback:
The text was updated successfully, but these errors were encountered: