Ticket #9061: potential-solution.patch

File potential-solution.patch, 2.0 KB (added by Daniel Ward, 4 years ago)

Potential solution by adding in 'can_delete_extra' option

  • django/forms/formsets.py

    diff --git django/forms/formsets.py django/forms/formsets.py
    index eafe357..1f61ec9 100644
    class BaseFormSet(object): 
    362362
    363363    def add_fields(self, form, index):
    364364        """A hook for adding extra fields on to each form instance."""
     365        initial_form_count = self.initial_form_count()
    365366        if self.can_order:
    366367            # Only pre-fill the ordering field for initial forms.
    367             if index is not None and index < self.initial_form_count():
     368            if index is not None and index < initial_form_count:
    368369                form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_('Order'), initial=index + 1, required=False)
    369370            else:
    370371                form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_('Order'), required=False)
    371         if self.can_delete:
     372        if self.can_delete and (self.can_delete_extra or index < initial_form_count):
    372373            form.fields[DELETION_FIELD_NAME] = BooleanField(label=_('Delete'), required=False)
    373374
    374375    def add_prefix(self, index):
    class BaseFormSet(object): 
    414415
    415416def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
    416417                    can_delete=False, max_num=None, validate_max=False,
    417                     min_num=None, validate_min=False):
     418                    min_num=None, validate_min=False, can_delete_extra=True):
    418419    """Return a FormSet for the given form class."""
    419420    if min_num is None:
    420421        min_num = DEFAULT_MIN_NUM
    def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False, 
    426427    absolute_max = max_num + DEFAULT_MAX_NUM
    427428    attrs = {'form': form, 'extra': extra,
    428429             'can_order': can_order, 'can_delete': can_delete,
     430             'can_delete_extra': can_delete_extra,
    429431             'min_num': min_num, 'max_num': max_num,
    430432             'absolute_max': absolute_max, 'validate_min': validate_min,
    431433             'validate_max': validate_max}
Back to Top