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

ModelChoiceFilter's queryset kwarg attached to memoized fields - which can cause issues #1525

Open
shangxiao opened this issue Aug 11, 2022 · 6 comments

Comments

@shangxiao
Copy link

shangxiao commented Aug 11, 2022

Given the following filterset (details omitted for brevity):

def get_bar(request):
    return Bar.objects.filter(…) if request else Bar.objects.none()
    
class FooFilterSet(FilterSet):
    bar = ModelChoiceFilter(queryset=get_bar)

If you happen to access base_filters at module level and (or anywhere without a request) like so:

FooFilterSet.base_filters['bar'].field

Then the bar queryset is evaluated and returns an empty queryset. Subsequently when you process the filtering during an actual request the filtered queryset never gets attached to the form field.

@carltongibson
Copy link
Owner

@shangxiao Any chance you could open a branch with a failing test case so I can see exactly what you mean? Thanks.

@shangxiao
Copy link
Author

@carltongibson cheers for responding so quickly 👍 Added a PR with test showing what I meant.

@shangxiao
Copy link
Author

Honestly I'm not really expecting a fix per se but just wanted to "document" it with an issue so that others may be aware. Perhaps mentioning this in the docs might be worthwhile?

@carltongibson
Copy link
Owner

Thanks @shangxiao. Let me have a think about it, but yes... forcing evaluation of base_filters this way may not be something we're going to want to work around. 🤔

@shangxiao
Copy link
Author

One thing we might do here is just deepcopy base_filters to introspect it.

@shangxiao
Copy link
Author

Actually also worth mentioning that introspecting filters like so works fine:

FooFilterSet().filters['bar'].field

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

2 participants