Change the request for a set of ModelChoiceField in __init__ forms. The form

I am currently having a problem with overriding __init__()

for forms.Form

.

Basic form

class ReportsMainForm(forms.Form):
 ---- #Some fields
    def __init__(self, *args, **kwargs):
        super(ReportsMainForm, self).__init__(*args, **kwargs)

      

Children's uniform

class Child(ReportsMainForm):
    customer = forms.ModelChoiceField(
        queryset=Customer.objects.none(), label="Customer", empty_label=None, required=False)

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(Child, self).__init__(*args, **kwargs)
        self.fields['customer'].queryset = Customer.objects.filter(user=self.request.user)

      

Problem

The problem is mine is queryset

not up to date. What am I missing?

+3


source to share


2 answers


Try changing also the request set widget (ehm ...):

self.fields['customer'].queryset = ...
self.fields['customer'].widget.choices = self.fields['customer'].choices

      

Why?



Checking the code (see django.forms.model.ModelChoiceField) when a set of queries is given in the field widget selection is also updated (which is good):

But the selection is cached in the field and therefore they are always the same ...

I think this is a bug, as there is an explicit "cache_choices" parameter in the init field, the default is False.

+3


source


this line is wrong

super(ProjectsBySalesman, self).__init__(*args, **kwargs)

      



it should be

super(Child, self).__init__(*args, **kwargs)

      

0


source







All Articles