mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Made the semi-private _max_form_count live on the public API of formsets by renaming it to max_num. This also removes the ManagementForm use of MAX_COUNT since that usage should just be referenced to the formset's max_num property. Refs #7899. Thanks Peter of the Norse for straightening me out.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8058 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -10,7 +10,6 @@ __all__ = ('BaseFormSet', 'all_valid')
|
||||
# special field names
|
||||
TOTAL_FORM_COUNT = 'TOTAL_FORMS'
|
||||
INITIAL_FORM_COUNT = 'INITIAL_FORMS'
|
||||
MAX_FORM_COUNT = 'MAX_FORMS'
|
||||
ORDERING_FIELD_NAME = 'ORDER'
|
||||
DELETION_FIELD_NAME = 'DELETE'
|
||||
|
||||
@@ -23,7 +22,6 @@ class ManagementForm(Form):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
|
||||
self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
|
||||
self.base_fields[MAX_FORM_COUNT] = IntegerField(widget=HiddenInput)
|
||||
super(ManagementForm, self).__init__(*args, **kwargs)
|
||||
|
||||
class BaseFormSet(StrAndUnicode):
|
||||
@@ -47,23 +45,21 @@ class BaseFormSet(StrAndUnicode):
|
||||
if self.management_form.is_valid():
|
||||
self._total_form_count = self.management_form.cleaned_data[TOTAL_FORM_COUNT]
|
||||
self._initial_form_count = self.management_form.cleaned_data[INITIAL_FORM_COUNT]
|
||||
self._max_form_count = self.management_form.cleaned_data[MAX_FORM_COUNT]
|
||||
else:
|
||||
raise ValidationError('ManagementForm data is missing or has been tampered with')
|
||||
else:
|
||||
if initial:
|
||||
self._initial_form_count = len(initial)
|
||||
if self._initial_form_count > self._max_form_count and self._max_form_count > 0:
|
||||
self._initial_form_count = self._max_form_count
|
||||
if self._initial_form_count > self.max_num and self.max_num > 0:
|
||||
self._initial_form_count = self.max_num
|
||||
self._total_form_count = self._initial_form_count + self.extra
|
||||
else:
|
||||
self._initial_form_count = 0
|
||||
self._total_form_count = self.extra
|
||||
if self._total_form_count > self._max_form_count and self._max_form_count > 0:
|
||||
self._total_form_count = self._max_form_count
|
||||
if self._total_form_count > self.max_num and self.max_num > 0:
|
||||
self._total_form_count = self.max_num
|
||||
initial = {TOTAL_FORM_COUNT: self._total_form_count,
|
||||
INITIAL_FORM_COUNT: self._initial_form_count,
|
||||
MAX_FORM_COUNT: self._max_form_count}
|
||||
INITIAL_FORM_COUNT: self._initial_form_count}
|
||||
self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
|
||||
|
||||
# construct the forms in the formset
|
||||
@@ -280,7 +276,7 @@ def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
|
||||
"""Return a FormSet for the given form class."""
|
||||
attrs = {'form': form, 'extra': extra,
|
||||
'can_order': can_order, 'can_delete': can_delete,
|
||||
'_max_form_count': max_num}
|
||||
'max_num': max_num}
|
||||
return type(form.__name__ + 'FormSet', (formset,), attrs)
|
||||
|
||||
def all_valid(formsets):
|
||||
|
||||
@@ -305,8 +305,8 @@ class BaseModelFormSet(BaseFormSet):
|
||||
queryset=None, **kwargs):
|
||||
self.queryset = queryset
|
||||
defaults = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
|
||||
if self._max_form_count > 0:
|
||||
qs = self.get_queryset()[:self._max_form_count]
|
||||
if self.max_num > 0:
|
||||
qs = self.get_queryset()[:self.max_num]
|
||||
else:
|
||||
qs = self.get_queryset()
|
||||
defaults['initial'] = [model_to_dict(obj) for obj in qs]
|
||||
|
||||
Reference in New Issue
Block a user