On Sun, Jun 12, 2022 at 11:46:44AM -0700, Sylvain wrote: > 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())
Can you post how you're using the form? Are you using the form in a view? Can you post the view code? -- 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/20220612211356.GB32625%40fattuba.com.

