Ticket #18166: django-formset-form-kwargs.diff
File django-formset-form-kwargs.diff, 2.1 KB (added by , 13 years ago) |
---|
-
django/forms/formsets.py
8 8 from django.utils.encoding import StrAndUnicode 9 9 from django.utils.safestring import mark_safe 10 10 from django.utils.translation import ugettext as _ 11 from copy import copy 11 12 12 13 13 __all__ = ('BaseFormSet', 'all_valid') 14 14 15 15 # special field names … … 36 36 A collection of instances of the same Form class. 37 37 """ 38 38 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): 40 40 self.is_bound = data is not None or files is not None 41 41 self.prefix = prefix or self.get_default_prefix() 42 42 self.auto_id = auto_id … … 46 46 self.error_class = error_class 47 47 self._errors = None 48 48 self._non_form_errors = None 49 self.form_kwargs = form_kwargs or { } 49 50 # construct the forms in the formset 50 51 self._construct_forms() 51 52 … … 112 113 # instantiate all the forms and put them in self.forms 113 114 self.forms = [] 114 115 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)) 116 123 117 124 def _construct_form(self, i, **kwargs): 118 125 """ … … 135 142 self.add_fields(form, i) 136 143 return form 137 144 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 138 151 def _get_initial_forms(self): 139 152 """Return a list of all the initial forms in this formset.""" 140 153 return self.forms[:self.initial_form_count()]