Ticket #9532: formsets_min_num.diff

File formsets_min_num.diff, 3.1 KB (added by Gabriel Farrell, 16 years ago)
  • django/forms/formsets.py

     
    5959                self._total_form_count = self.extra
    6060            if self._total_form_count > self.max_num and self.max_num > 0:
    6161                self._total_form_count = self.max_num
     62            if self._total_form_count < self.min_num:
     63                self._total_form_count = self.min_num
    6264            initial = {TOTAL_FORM_COUNT: self._total_form_count,
    6365                       INITIAL_FORM_COUNT: self._initial_form_count}
    6466            self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
     
    273275        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
    274276
    275277def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
    276                     can_delete=False, max_num=0):
     278                    can_delete=False, max_num=0, min_num=0):
    277279    """Return a FormSet for the given form class."""
    278280    attrs = {'form': form, 'extra': extra,
    279281             'can_order': can_order, 'can_delete': can_delete,
    280              'max_num': max_num}
     282             'max_num': max_num, 'min_num': min_num}
    281283    return type(form.__name__ + 'FormSet', (formset,), attrs)
    282284
    283285def all_valid(formsets):
  • django/forms/models.py

     
    437437def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
    438438                         formset=BaseModelFormSet,
    439439                         extra=1, can_delete=False, can_order=False,
    440                          max_num=0, fields=None, exclude=None):
     440                         max_num=0, min_num=0, fields=None, exclude=None):
    441441    """
    442442    Returns a FormSet class for the given Django model class.
    443443    """
    444444    form = modelform_factory(model, form=form, fields=fields, exclude=exclude,
    445445                             formfield_callback=formfield_callback)
    446446    FormSet = formset_factory(form, formset, extra=extra, max_num=max_num,
    447                               can_order=can_order, can_delete=can_delete)
     447                              min_num=min_num, can_order=can_order,
     448                              can_delete=can_delete)
    448449    FormSet.model = model
    449450    return FormSet
    450451
     
    538539                          formset=BaseInlineFormSet, fk_name=None,
    539540                          fields=None, exclude=None,
    540541                          extra=3, can_order=False, can_delete=True, max_num=0,
    541                           formfield_callback=lambda f: f.formfield()):
     542                          min_num=0, formfield_callback=lambda f: f.formfield()):
    542543    """
    543544    Returns an ``InlineFormSet`` for the given kwargs.
    544545
     
    566567        'fields': fields,
    567568        'exclude': exclude,
    568569        'max_num': max_num,
     570        'min_num': min_num,
    569571    }
    570572    FormSet = modelformset_factory(model, **kwargs)
    571573    FormSet.fk = fk
Back to Top