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
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/99b62ef4-de2c-45c2-9bec-0d5dcfdb6980n%40googlegroups.com.