When I use translation.get_language() in the queryset attribute of a class based view it returns an incorrect value when changing the language.
class LandingPageOverview(ListView):
model = LandingPage
context_object_name = 'landingpages'
template_name = 'landingpage/overview.html'
queryset = LandingPage.objects.filter(language=translation.get_language())
When I use get_queryset the right value is returned.
class LandingPageOverview(ListView):
model = LandingPage
context_object_name = 'landingpages'
template_name = 'landingpage/overview.html'
def get_queryset(self):
return LandingPage.objects.filter(language=translation.get_language())
Can anyone explain why?
In the first example, the language is evaluated when class is loaded at first time. Try for example this:
result will be:
EDIT:
you could do something like this:
and use it like you want: