The form is used in a view, yes, like so:

def objects_list(request):
    if request.method == "POST" and "delete" in request.POST:
        delete_form = MyForm(data=request.POST)

        if delete_form.is_valid():
            if delete_form.cleaned_data["confirm"]:
                delete_form.save()
                return redirect(...)
            else:
                return render(request, "delete_confirm.html", {"form": 
delete_form})
    ...
On Sunday, June 12, 2022 at 11:14:41 PM UTC+2 Ryan Nowakowski wrote:

> 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/f4261d4a-5417-4e42-8f4b-08aaf92ca67dn%40googlegroups.com.

Reply via email to