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

pass additional variable form views.py to filters.py #870

Closed
mgausvoe opened this issue Feb 6, 2018 · 2 comments
Closed

pass additional variable form views.py to filters.py #870

mgausvoe opened this issue Feb 6, 2018 · 2 comments

Comments

@mgausvoe
Copy link

mgausvoe commented Feb 6, 2018

to show the specific possible choices of a dataset I need to pass an additional parameter (here my_variable) form the views.py to filters.py:

views.py:

my_variable= 123

f = CompanyDataFilter(queryset=CompanyDataView.objects.using('extern_db').filter(company_id=primary))

filters.py

class CompanyDataFilter(django_filters.FilterSet):
....

sector = django_filters.ModelChoiceFilter(queryset=CompanyDataView.objects.values_list('sector', flat=True).using('extern_db').distinct().filter(company_id=my_variable),empty_label='Sector', help_text='Search Sector')

....

but I have no idea how to manage it to pass my_variable from the views.py to the filters.py

Any idea?

Thanks a lot in advance!

@jorgii
Copy link

jorgii commented May 18, 2018

I am searching for a similar solution as well. I need to pass additional context to the filter class.
In my case I use django rest framework.
What I can suggest is to add context attribute to the filter class which can be passed by a get_filter_context method in the view. Pretty similar to how context is passed to the serializer - http://www.django-rest-framework.org/api-guide/serializers/#including-extra-context

@carltongibson
Copy link
Owner

Closing as per #910 (comment)

Also see #865

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

3 participants