Ticket #5923: required-forms-count-field.diff

File required-forms-count-field.diff, 3.0 KB (added by jakub_vysoky, 17 years ago)

required forms count field added into formsets

  • django/newforms/formsets.py

    commit 2800d34f426b9d9ee5637990d95658cebddfe2f1
    Author: Jakub Vysoky <kvbik@stone.(none)>
    Date:   Mon Oct 22 18:50:51 2007 +0200
    
        required forms count field added into formsets
    
    diff --git a/django/newforms/formsets.py b/django/newforms/formsets.py
    index 56179a9..af336c7 100644
    a b __all__ = ('BaseFormSet', 'formset_for_form', 'all_valid')  
    77
    88# special field names
    99FORM_COUNT_FIELD_NAME = 'COUNT'
     10REQUIRED_FORMS_COUNT_FIELD_NAME = 'REQUIRED'
    1011ORDERING_FIELD_NAME = 'ORDER'
    1112DELETION_FIELD_NAME = 'DELETE'
    1213
    class ManagementForm(Form):  
    1718    increment the count field of this form as well.
    1819    """
    1920    def __init__(self, *args, **kwargs):
     21        self.base_fields[REQUIRED_FORMS_COUNT_FIELD_NAME] = IntegerField(widget=HiddenInput)
    2022        self.base_fields[FORM_COUNT_FIELD_NAME] = IntegerField(widget=HiddenInput)
    2123        super(ManagementForm, self).__init__(*args, **kwargs)
    2224
    class BaseFormSet(object):  
    3739            self.management_form = ManagementForm(data, files, auto_id=self.auto_id, prefix=self.prefix)
    3840            if self.management_form.is_valid():
    3941                self.total_forms = self.management_form.cleaned_data[FORM_COUNT_FIELD_NAME]
    40                 self.required_forms = self.total_forms - self.num_extra
    41                 self.change_form_count = self.total_forms - self.num_extra
     42                self.required_forms = self.management_form.cleaned_data[REQUIRED_FORMS_COUNT_FIELD_NAME]
     43                self.change_form_count = self.required_forms
    4244            else:
    4345                # not sure that ValidationError is the best thing to raise here
    4446                raise ValidationError('ManagementForm data is missing or has been tampered with')
    class BaseFormSet(object):  
    4648            self.change_form_count = len(initial)
    4749            self.required_forms = len(initial)
    4850            self.total_forms = self.required_forms + self.num_extra
    49             self.management_form = ManagementForm(initial={FORM_COUNT_FIELD_NAME: self.total_forms}, auto_id=self.auto_id, prefix=self.prefix)
     51            initial = {
     52                    FORM_COUNT_FIELD_NAME: self.total_forms,
     53                    REQUIRED_FORMS_COUNT_FIELD_NAME: self.required_forms,
     54            }
     55            self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
    5056        else:
    5157            self.change_form_count = 0
    5258            self.required_forms = 0
    5359            self.total_forms = self.num_extra
    54             self.management_form = ManagementForm(initial={FORM_COUNT_FIELD_NAME: self.total_forms}, auto_id=self.auto_id, prefix=self.prefix)
     60            initial = {
     61                    FORM_COUNT_FIELD_NAME: self.total_forms,
     62                    REQUIRED_FORMS_COUNT_FIELD_NAME: self.required_forms,
     63            }
     64            self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
    5565
    5666    def _get_add_forms(self):
    5767        """Return a list of all the add forms in this ``FormSet``."""
Back to Top