How do I let forms.models.ModelChoiceField.queryset relate on request.user?

120 views
Skip to first unread message

Axel Rau

unread,
Nov 1, 2015, 1:52:35 PM11/1/15
to django...@googlegroups.com
User should see only choices related to him in a ModelChoiceField.
Do I need a fresh form per request?
What would be the best approach?

Thanks, Axel
---
PGP-Key:29E99DD6 ☀ +49 160 9945 7889 ☀ computing @ chaos claudius

Vijay Khemlani

unread,
Nov 2, 2015, 7:50:57 AM11/2/15
to django...@googlegroups.com
At least what I do in those cases is to add a constructor (__init__) to the form class which takes the user as a parameter, modify the choicefield queryset, and then call the original constructor of the form.


--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/728C3EDF-62BB-4450-920B-BB7376417D9C%40Chaos1.DE.
For more options, visit https://groups.google.com/d/optout.

Axel Rau

unread,
Nov 4, 2015, 6:53:21 AM11/4/15
to django...@googlegroups.com
Thanks, I will try this,
Axel
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei0v3POcwmw9s0beYyGXK15ybwDPooPsYDm%3D1_0XvpStpQ%40mail.gmail.com.

Axel Rau

unread,
Nov 16, 2015, 12:39:57 PM11/16/15
to django...@googlegroups.com
Any idea how to add __init__() to a form class, created by modelform_factory () ?

Axel

Am 02.11.2015 um 13:50 schrieb Vijay Khemlani <vkhe...@gmail.com>:


For more options, visit https://groups.google.com/d/optout.

Axel Rau

unread,
Nov 17, 2015, 2:22:29 PM11/17/15
to django...@googlegroups.com
Instead off writing an __init__ for the form class, I ended up with this get_form()
in my view class:

    def get_form(self, form_class=None):
        form = self.get_form_class()
        pk = self.request.user.pk
        self.object = Account.objects.get(pk=pk)
        form_instance = form(instance=self.object)
        self.filter_modelChoices(form_instance)
        return form_instance
    
    def filter_modelChoices(self, form_instance):
        """
        Changes the model choice fields to be correctly filtered
         as required by request.user.
        """
        opts = self.model._meta
        for formfield_name in form_instance.fields:
            formfield = form_instance.fields[formfield_name]
            if isinstance(formfield, ModelChoiceField):
                [modelfield] = 
                    [f for f in opts.fields if f.name == formfield_name]
                qs = userVisibleFilterQS(
                        self.request.user.pk
                        False,
                        modelfield.related_model())
                if qs:
                    formfield.queryset = formfield.queryset.filter(qs)


Am 16.11.2015 um 18:38 schrieb Axel Rau <Axel...@chaos1.de>:

Any idea how to add __init__() to a form class, created by modelform_factory () ?

Axel

Tom Evans

unread,
Nov 18, 2015, 9:29:20 AM11/18/15
to django...@googlegroups.com
On Mon, Nov 16, 2015 at 5:38 PM, Axel Rau <Axel...@chaos1.de> wrote:
>
> Any idea how to add __init__() to a form class, created by modelform_factory () ?
>
> Axel
>

modelform_factory takes many arguments, one of which is for the base
form class to use. You can provide a different base class from the
default ModelForm, make sure to derive the new base class from
ModelForm.

https://docs.djangoproject.com/en/1.8/ref/forms/models/#django.forms.models.modelform_factory

Cheers

Tom

Axel Rau

unread,
Nov 18, 2015, 3:13:18 PM11/18/15
to 'Tom Evans' via Django users

> Am 18.11.2015 um 15:28 schrieb 'Tom Evans' via Django users <django...@googlegroups.com>:
>
> modelform_factory takes many arguments, one of which is for the base
> form class to use. You can provide a different base class from the
> default ModelForm, make sure to derive the new base class from
> ModelForm.
>
Ah, thanks for the explanation,
Reply all
Reply to author
Forward
0 new messages