How to add a form as field attribute in a django ModelForm
<p>I have a ModelForm for a Product object set up like this: </p>

<pre><code>class ProductForm(forms.ModelForm):
    compositon_choices = ((2L, u'Calcium (100mg)'), (3L, u'Iron (500mg)'))
        composition_selection = forms.\
              MultipleChoiceField(widget=forms.CheckboxSelectMultiple,      
                        choices=compositon_choices )

    class Meta:
        model = Product
        fields = [
            'title', 'title_de', 'title_es', 'upc', 'description', 
            'description_en_gb', 'description_de',
            'description_es', 'is_discountable', 'structure',   
            'unit_type', 'product_concentration',]

        widgets = {
            'structure': forms.HiddenInput()
        }
</code></pre>

<p>In the example above I extended the ModelForm with a MultipleChoiceField by adding the composition_selection field (this works): </p>

<p>I would like the composoition_selection to be a form itself and not just a MultipleChoiceField: </p>

<pre><code>class ProductComponentForm(forms.Form):
    component_amount = forms.IntegerField()
    component_name = forms.CharField()
</code></pre>

<p>and then extend the ModelForm with this new form like this:</p>

<pre><code>class ProductForm(forms.ModelForm):
        composition_selection = ProductComponentForm()

        class Meta:
            model = Product
            fields = [
                'title', 'title_de', 'title_es', 'upc', 'description', 
                'description_en_gb', 'description_de',
                'description_es', 'is_discountable', 'structure',   
                'unit_type', 'product_concentration',]

            widgets = {
                'structure': forms.HiddenInput()
            } 
</code></pre>

<p>But I cannot get this to work. This ProductForm that I want to create never gets rendered,and nothing appears. Am I doing something wrong or missing something? What would be the best way to extend a ModelForm with a SubForm? </p>

<p>Finally I understand what I did wrong. To make subforms in Django one needs formsets. 
In my case i needed two different types of formsets because I had two different relationships that I wanted to change from one form. </p>

<ul>
<li>one to many relationship</li>
<li>many to many relationship</li>
</ul>

<p>depending which side of the relationship and which relationship type one wants to edit from within a single form there are different approaches: </p>

<p>There is the inlineformset_factory:
<a href="https://docs.djangoproject.com/el/1.10/topics/forms/modelforms/#inline-formsets" rel="nofollow">https://docs.djangoproject.com/el/1.10/topics/forms/modelforms/#inline-formsets</a> . This type of formset is used when one wants to edit the <strong>many</strong> side of a one to many relationship</p>

<p>If one wants to edit the one side of a one to many relationship the 
modelformset_factory gets used: 
<a href="https://docs.djangoproject.com/el/1.10/topics/forms/modelforms/#model-formsets" rel="nofollow">https://docs.djangoproject.com/el/1.10/topics/forms/modelforms/#model-formsets</a></p>

<p>one can create a model_formset, and then add this formset to the main formset of the main form. </p>

<p>when one wants to edit a many to many relationship within a single form, an inline_formset_factory can be created with the intermediary table of the many to many relationship. </p>

<p>this formset than can be added to main form. </p>

<p>For my use case that I described above I ended up using a model_formset_factory and added that to the main form. </p>

python django django-models django-forms