﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29739	BaseModelFormSet ignores excess rows in initial when extra < len(initial)	Mark Gensler	nobody	"When passing initial data through `BaseModelFormSet.__init__(..., initial=)` and the length of the initial data exceeds the number of extra forms defined in `modelformsetfactory(..., extra=)`, then the data in the range `self.initial_extra[self.extra:]` will be ignored.

E.g.

{{{#!python
class Person(models.Model):
    name = models.CharField(max_length=100)

initial = [{'name': 'Foo'}, {'name': 'Bar'}, {'name': 'Baz'}]
PersonFormSet = modelformsetfactory(Person, extra=2)
formset = MyModelFormSet(initial=initial)
}}}
Now, assuming that `models.Person.objects.all()` is empty, 
{{{#!python
len(formset.forms) == 2
True
}}}
This is because `BaseFormSet.total_form_count()` blindly uses self.extra and does not take into account the additional data in `self.initial_extra`.

In order to work around this users would check `len(initial)` before constructing the class and modify the `extra` kwarg to be large enough. This causes issues when writing abstract code which constructs the class separately from initialising it, or reuses the same class with differing initial data. In these cases the length of the initial data may not be known at the time of class construction.

Possible solutions are the number of extra forms equals `self.extra + len(self.initial_extra)` or `max(self.extra, len(self.initial_extra))`."	Bug	closed	Forms	2.1	Normal	duplicate	modelformset		Unreviewed	0	0	0	0	0	0
