[Answer]-Django class based view, wrong language value when using 'queryset' attribute

1πŸ‘

βœ…

In the first example, the language is evaluated when class is loaded at first time. Try for example this:

def a(x=[]):
    x.append(1)
    print x


a()
a()
a()
a()

result will be:

[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]

EDIT:

you could do something like this:

class LandingPageOverview(ListView):
    model = LandingPage
    context_object_name = 'landingpages'
    template_name = 'landingpage/overview.html'

    @property
    def queryset(self):
        return LandingPage.objects.filter(language=translation.get_language())

and use it like you want:

l = LandingPageOverview()
l.queryset

Leave a comment