Ticket #18166: django-formset-form-kwargs.diff

File django-formset-form-kwargs.diff, 2.1 KB (added by tevans, 13 years ago)

Patch implementing features described in ticket

  • django/forms/formsets.py

     
    88from django.utils.encoding import StrAndUnicode
    99from django.utils.safestring import mark_safe
    1010from django.utils.translation import ugettext as _
     11from copy import copy
    1112
    12 
    1313__all__ = ('BaseFormSet', 'all_valid')
    1414
    1515# special field names
     
    3636    A collection of instances of the same Form class.
    3737    """
    3838    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    39                  initial=None, error_class=ErrorList):
     39                 initial=None, error_class=ErrorList, form_kwargs=None):
    4040        self.is_bound = data is not None or files is not None
    4141        self.prefix = prefix or self.get_default_prefix()
    4242        self.auto_id = auto_id
     
    4646        self.error_class = error_class
    4747        self._errors = None
    4848        self._non_form_errors = None
     49        self.form_kwargs = form_kwargs or { }
    4950        # construct the forms in the formset
    5051        self._construct_forms()
    5152
     
    112113        # instantiate all the forms and put them in self.forms
    113114        self.forms = []
    114115        for i in xrange(self.total_form_count()):
    115             self.forms.append(self._construct_form(i))
     116            per_form_kwargs = self._kwargs_for_form(i)
     117            form_kwargs = { }
     118            if self.form_kwargs:
     119                form_kwargs = copy(self.form_kwargs)
     120            if per_form_kwargs:
     121                form_kwargs.update(per_form_kwargs)
     122            self.forms.append(self._construct_form(i, **form_kwargs))
    116123
    117124    def _construct_form(self, i, **kwargs):
    118125        """
     
    135142        self.add_fields(form, i)
    136143        return form
    137144
     145    def _kwargs_for_form(self, i):
     146        """
     147        Generate the kwargs to instantiate the i-th form instance in a formset.
     148        """
     149        return { }
     150
    138151    def _get_initial_forms(self):
    139152        """Return a list of all the initial forms in this formset."""
    140153        return self.forms[:self.initial_form_count()]
Back to Top