Tuesday, December 27, 2016

What determines initial number of forms in Django modelformset_factory?

My model formset is producing an initial 7 forms, as seen here:

<input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="7" /><input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="7" />

I would like to alter that number, but cannot see how.
view and form for ref:

def micro_log_create(request):
    MicroLogFormSet = modelformset_factory(Micro_Log, form=MicroLogForm, max_num=4, extra=0)
    if request.method == 'POST':
        formset = MicroLogFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            return reverse('micro-log')
    else:
        formset = MicroLogFormSet()
    return render(request, 'app/micro_log/micro_log_create.html', {'formset': formset})

class MicroLogForm(forms.ModelForm):
    class Meta:
        model = Micro_Log   
        exclude = ['user', 'date_time']
MicroLogFormSet = modelformset_factory(Micro_Log, form=MicroLogForm, max_num=4, extra=0)
formset = MicroLogFormSet()

Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/96b7e213-0391-4444-a560-4b70f96aa608%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment