Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Inlines and Media Create a Formset

formset

Creating products one at a time is getting tedious. I want to be able to make multiple at a time. In forms.py, create a model formset factory for the Digital model. Include the same fields as the DigitalProductForm. Name the factory DigitalFormset.

products/forms.py
from django import forms

from . import models


class DigitalProductForm(forms.ModelForm):
    class Meta:
        model = models.Digital
        fields = ['name', 'description', 'url']


DigitalFormset = Forms.modelformset_factory(
    models.Digital, 
    form=DigitalForms, 
) 
    fields = ['name', 'description', 'url']

5 Answers

After a long struggle I then figured this out:

  1. your fields should be inside those parentheses;
  2. remove form = DigitalForm;

you should ahve something like this

DigitalFormset = forms.modelformset_factory( models.Digital, fields = ['name', 'description', 'url'] )

Humphrey,

All you have to do is set the variables extra and max_num = 5.

You can do it likes this:

DigitalFormset = forms.modelformset_factory( models.Digital, extra = 5, max_num = 5, fields = ['name', 'description', 'url'] )

thank you Innocent it worked

guys am stuck on task 2 of that challlenge

please help

Leroy Saldanha
Leroy Saldanha
8,006 Points

DigitalFormset = forms.modelformset_factory( models.Digital, fields = ['name', 'description', 'url'],extra =5,max_num=5 )