Ticket #7899: formset.diff
File formset.diff, 3.9 KB (added by , 16 years ago) |
---|
-
django/newforms/formsets.py
47 47 if self.management_form.is_valid(): 48 48 self._total_form_count = self.management_form.cleaned_data[TOTAL_FORM_COUNT] 49 49 self._initial_form_count = self.management_form.cleaned_data[INITIAL_FORM_COUNT] 50 self. _max_form_count = self.management_form.cleaned_data[MAX_FORM_COUNT]50 self.max_form_count = self.management_form.cleaned_data[MAX_FORM_COUNT] 51 51 else: 52 52 raise ValidationError('ManagementForm data is missing or has been tampered with') 53 53 else: 54 54 if initial: 55 55 self._initial_form_count = len(initial) 56 if self._initial_form_count > self. _max_form_count and self._max_form_count > 0:57 self._initial_form_count = self. _max_form_count56 if self._initial_form_count > self.max_form_count and self.max_form_count > 0: 57 self._initial_form_count = self.max_form_count 58 58 self._total_form_count = self._initial_form_count + self.extra 59 59 else: 60 60 self._initial_form_count = 0 61 61 self._total_form_count = self.extra 62 if self._total_form_count > self. _max_form_count and self._max_form_count > 0:63 self._total_form_count = self. _max_form_count62 if self._total_form_count > self.max_form_count and self.max_form_count > 0: 63 self._total_form_count = self.max_form_count 64 64 initial = {TOTAL_FORM_COUNT: self._total_form_count, 65 65 INITIAL_FORM_COUNT: self._initial_form_count, 66 MAX_FORM_COUNT: self. _max_form_count}66 MAX_FORM_COUNT: self.max_form_count} 67 67 self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix) 68 68 69 69 # construct the forms in the formset … … 280 280 """Return a FormSet for the given form class.""" 281 281 attrs = {'form': form, 'extra': extra, 282 282 'can_order': can_order, 'can_delete': can_delete, 283 ' _max_form_count': max_num}283 'max_form_count': max_num} 284 284 return type(form.__name__ + 'FormSet', (formset,), attrs) 285 285 286 286 def all_valid(formsets): -
django/newforms/models.py
306 306 queryset=None, **kwargs): 307 307 self.queryset = queryset 308 308 defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix} 309 if self. _max_form_count > 0:310 qs = self.get_queryset()[:self. _max_form_count]309 if self.max_form_count > 0: 310 qs = self.get_queryset()[:self.max_form_count] 311 311 else: 312 312 qs = self.get_queryset() 313 313 defaults['initial'] = [model_to_dict(obj) for obj in qs] … … 402 402 403 403 class BaseInlineFormset(BaseModelFormSet): 404 404 """A formset for child objects related to a parent.""" 405 def __init__(self, data=None, files=None, instance=None, save_as_new=False ):405 def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None): 406 406 from django.db.models.fields.related import RelatedObject 407 407 self.instance = instance 408 408 self.save_as_new = save_as_new 409 409 # is there a better way to get the object descriptor? 410 410 self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name() 411 super(BaseInlineFormset, self).__init__(data, files, prefix= self.rel_name)411 super(BaseInlineFormset, self).__init__(data, files, prefix=(prefix or self.rel_name)) 412 412 413 413 def _construct_forms(self): 414 414 if self.save_as_new: