Hello,
I’m trying to use the current language of the user in a queryset, but the value gets resolved outside of the request-response cycle. I thought this could work since querysets are lazy, but I guess that doesn’t make the values they use lazy. Here’s an example of what I’m talking about:
from django.utils.translation import get_language
class MyManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(language=get_language())
class MyModel(models.Model):
...
objects = MyManager()
Using this in a code path that’s in the request-response cycle works (eg. in a view), but using it in a form definition doesn’t (I get the default language instead of the one from the request):
class MyForm(forms.Form):
option = forms.ModelChoiceField(queryset=MyModel.objects.all())
Is there a way to get this working without having to move the queryset call to the form __init__? Using django.utils.functional.lazy doesn’t work either because the value needs to be resolved in the filter.
Thanks!
Sylvain