Why django calls __init __ () twice for form fields

I am trying to use django-simple-autocomplete in a form. However, when I add debugging fingerprints to simple_autocomplete.widgets, I see that for each widget the form fields are __init__()called twice, first with the parameters specified in the form specification, and the second time without any arguments that obviously violate everything that the parameter defines .

I need to get around this by doing something like the following:

class MyForm(forms.Form):
    def __init__(self,*args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['foo'] = AutoCompleteWidget(url="/json_url")

    foo = forms.ModelChoiceField(
        widget=None,
        ....
        )

Why is this so?

EDIT / CLARIFICATION :

  • this happens if I don't have the above workaround, but instead just specify the widget = MyWidget () in the definition of foo.
  • __init__(), .
  • @zubair89 - def __init__() - , !
+1

All Articles