Error when passing parameter to form
<p>I'm trying to pass a parameter to a form, in this case is an object_id.
The form gets used only on the <em>change_view</em>, this code works:</p>

<p>My form:</p>

<pre><code>class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.my_id = kwargs.pop('my_id', None)
        super(MyForm, self).__init__(*args, **kwargs)

class Meta:
    model = MyModel
    fields = ('thing_to_show_a',)
</code></pre>

<p>My admin model: </p>

<pre><code>class MyModelAdmin(admin.ModelAdmin):
    def change_view(self, request, obj_id):
        self.form = MyForm
        return super(MyModelAdmin, self).change_view(request, obj_id)
...
</code></pre>

<p>But if I try to pass the id as a parameter in the form:</p>

<pre><code>class MyModelAdmin(admin.ModelAdmin):
    def change_view(self, request, obj_id):
        self.form = MyForm(my_id=obj_id)
        return super(MyModelAdmin, self).change_view(request, obj_id)
...
</code></pre>

<p>I get:</p>

<pre><code>'MyForm' object has no attribute '__name__'
</code></pre>

<p>In <code>self.form = MyForm</code> you assign a class object to self.form.
In <code>self.form = MyForm(my_id=obj_id)</code> you instantiate an object of class MyForm and assign it to self.form. </p>

<p>Django expect to find a class in <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form" rel="nofollow">self.form</a>, not an instance.</p>

python django django-forms