Ticket #3295: groups_in_forms.patch
File groups_in_forms.patch, 1.8 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
31 31 def __new__(cls, name, bases, attrs): 32 32 fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] 33 33 fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) 34 for field_name, obj in attrs.items(): 35 if field_name.startswith('group_'): 36 name_func = field_name.replace('group_','') 37 new_class.add_to_class(name_func, obj) 38 delattr(new_class, field_name) 34 39 attrs['fields'] = SortedDictFromList(fields) 35 40 return type.__new__(cls, name, bases, attrs) 36 41 … … 171 176 association with the field named '__all__'. 172 177 """ 173 178 return self.clean_data 174 179 def _prepare_HTML_output(self, list_fileds, func_HTML): 180 oldfields = self.fields.copy() # save original fields 181 data = [] 182 for field in list_fileds: 183 if oldfields.has_key(field): 184 data.append( (field, oldfields[field]) ) 185 self.fields = SortedDictFromList(data) 186 output = func_HTML() 187 self.fields = oldfields # restore original fields 188 return output 189 190 def add_to_class(cls, name, value): 191 setattr(cls, '%s_as_p' % name, lambda self: self._prepare_HTML_output( value, self.as_p )) 192 setattr(cls, '%s_as_table' % name, lambda self: self._prepare_HTML_output_HTML_output( value, self.as_table )) 193 add_to_class = classmethod(add_to_class) 175 194 class Form(BaseForm): 176 195 "A collection of Fields, plus their associated data." 177 196 # This is a separate class from BaseForm in order to abstract the way