#29739: BaseModelFormSet ignores excess rows in initial when extra < len(initial) --------------------------------------------+------------------------------ Reporter: S. Dole Melipone | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 2.1 Severity: Normal | Keywords: modelformset Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | --------------------------------------------+------------------------------ 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 `max(len(initial), some_default_value)`. 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))`. -- Ticket URL: <https://code.djangoproject.com/ticket/29739> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/056.0074741fe29e9239d0853a35d01cc64d%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.