How do I determine the current language inside an OrderingFilter?

88 views
Skip to first unread message

Chris Vradis

unread,
Apr 12, 2019, 3:38:28 PM4/12/19
to django-filter

I have a FilterSet with an OrderingFilter as follows:

from django.utils.translation import get_language

import django_filters

class geographicEntityFilter(django_filters.FilterSet):

        ordering
= django_filters.OrderingFilter(
                fields
= (
                         
('displayedNames__' + get_language()[0:2],'name'),
                 
)
       
)


I use that Filter with a class-based view:

class GeographicEntityListView(FilterView):
       
def get_context_data(self, **kwargs):
                context
= super().get_context_data(**kwargs)
                f
= geographicEntityFilter(self.request.GET, queryset=self.get_queryset())
               
...


What I want to do is to set the field name depending on the currently selected language by the user. The get_language() method always returns "en" no matter what language the site is on. I assume I need to access the language via the request, but I can't find any documentation on that.

That works in other parts of my code with other FilterSets e.g. when I define a callable for the QuerySet of a ModelChoiceFilter, where the request is available.

But according to the documentation, the fields argument "accepts the ‘list of two-tuples’ syntax ...fields may also just be an iterable of strings". No mention about a callable.

Any help is really appreciated.

vold...@gmail.com

unread,
Apr 14, 2019, 3:08:57 PM4/14/19
to django-filter
Hi Chris,

I don't know if I fully understand your question, but hopefully this is a good suggestion to be able to use the request information:

I copied a util class 'PageFilteredTableView' from a blog into the application and I added request=self.request in function get_queryset.
Then I used the util class in my view and added the filter as filter_class to the view class. And then I could use the request information in get_queryset of the view.
For code example see below.

Best, Betty

class XXXView(PagedFilteredTableView):
filter_class = XXXFilter

    def get_queryset(self, **kwargs):
        queryset = super(XXXView, self).get_queryset()
queryset = queryset.filter(id=self.request.user.id)
return queryset

class PagedFilteredTableView(SingleTableView):
filter_class = None
formhelper_class = None
context_filter_name = 'filter'

def get_queryset(self, **kwargs):
qs = super(PagedFilteredTableView, self).get_queryset()
self.filter = self.filter_class(self.request.GET, queryset=qs,
request=self.request)
self.filter.form.helper = self.formhelper_class()
return self.filter.qs

def get_table(self, **kwargs):
table = super(PagedFilteredTableView, self).get_table()
# Original example online:
# RequestConfig(self.request, paginate={
# 'page': self.kwargs['page'],
# 'per_page': self.paginate_by}).configure(table)
RequestConfig(self.request, paginate={
'per_page': self.paginate_by}).configure(table)
return table

def get_context_data(self, **kwargs):
context = super(PagedFilteredTableView, self).get_context_data()
context[self.context_filter_name] = self.filter
return context


Op vrijdag 12 april 2019 21:38:28 UTC+2 schreef Chris Vradis:
Reply all
Reply to author
Forward
0 new messages