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

View queryset is not actually filtered #29

Open
seav opened this issue Aug 30, 2016 · 2 comments
Open

View queryset is not actually filtered #29

seav opened this issue Aug 30, 2016 · 2 comments

Comments

@seav
Copy link

seav commented Aug 30, 2016

A view that has both permission_filter_queryset and get_queryset() in the same class definition will fail to filter the queryset.

For example, if I have the following view class definition:

class DetailProjList(PermissionRequiredMixin, generic.ListView):
    permission_required = 'proj.list'
    permission_filter_queryset = ['proj.detail']

    def get_queryset(self):
        return Project.objects.all()

The list of projects displayed is not actually filtered. While PermissionRequiredMixin.has_permission() is able to construct self.filtered_queryset, this filtered queryset is not called by the view rendering process because DetailProjList's get_queryset() method masks BasePermissionRequiredMixin's get_queryset() method.

It seems filtered querysets only work if the desired get_queryset() is only inherited via a mixin or a parent class and not declared in the view class itself.

@ian-ross
Copy link
Contributor

Let me think about this. I usually think of get_queryset as one of Django's DO WHAT I SAY methods, in the sense that, if you override it in a base view class, you are saying very clearly what you want the queryset to be, irrespective of whatever else is going on. I don't know if that's the right way to think of it.

What's the use case for this anyway? I don't think there are any instances of this in the current platform code.

@seav
Copy link
Author

seav commented Aug 30, 2016

I encountered this during my current coding. I wanted to override the get_queryset of a mixin, which returns a list of resources attached to the project, to instead return all resources in a project (whether attached to the project or not) in one particular view. To avoid this bug, I ended up just modifying the mixin itself to behave differently based on a new view class attribute.

Of course I can't do that if the mixin is not my code, so a workaround is to create a wrapper parent class, but that sounds like a kludge.

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

No branches or pull requests

2 participants