mirror of
https://github.com/django/django.git
synced 2025-10-24 22:26:08 +00:00
Fixed #13023 - Removed ambiguity with regard to the max_num option of formsets and as a result of admin inlines. Thanks to Gabriel Hurley for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12872 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -25,7 +25,7 @@ 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_NUM_FORM_COUNT] = IntegerField(widget=HiddenInput)
|
||||
self.base_fields[MAX_NUM_FORM_COUNT] = IntegerField(required=False, widget=HiddenInput)
|
||||
super(ManagementForm, self).__init__(*args, **kwargs)
|
||||
|
||||
class BaseFormSet(StrAndUnicode):
|
||||
@@ -69,8 +69,13 @@ class BaseFormSet(StrAndUnicode):
|
||||
if self.data or self.files:
|
||||
return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
|
||||
else:
|
||||
total_forms = self.initial_form_count() + self.extra
|
||||
if total_forms > self.max_num > 0:
|
||||
initial_forms = self.initial_form_count()
|
||||
total_forms = initial_forms + self.extra
|
||||
# Allow all existing related objects/inlines to be displayed,
|
||||
# but don't allow extra beyond max_num.
|
||||
if initial_forms > self.max_num >= 0:
|
||||
total_forms = initial_forms
|
||||
elif total_forms > self.max_num >= 0:
|
||||
total_forms = self.max_num
|
||||
return total_forms
|
||||
|
||||
@@ -81,7 +86,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
else:
|
||||
# Use the length of the inital data if it's there, 0 otherwise.
|
||||
initial_forms = self.initial and len(self.initial) or 0
|
||||
if initial_forms > self.max_num > 0:
|
||||
if initial_forms > self.max_num >= 0:
|
||||
initial_forms = self.max_num
|
||||
return initial_forms
|
||||
|
||||
@@ -324,7 +329,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
|
||||
|
||||
def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
|
||||
can_delete=False, max_num=0):
|
||||
can_delete=False, max_num=None):
|
||||
"""Return a FormSet for the given form class."""
|
||||
attrs = {'form': form, 'extra': extra,
|
||||
'can_order': can_order, 'can_delete': can_delete,
|
||||
|
||||
@@ -448,10 +448,10 @@ class BaseModelFormSet(BaseFormSet):
|
||||
if not qs.ordered:
|
||||
qs = qs.order_by(self.model._meta.pk.name)
|
||||
|
||||
if self.max_num > 0:
|
||||
self._queryset = qs[:self.max_num]
|
||||
else:
|
||||
self._queryset = qs
|
||||
# Removed queryset limiting here. As per discussion re: #13023
|
||||
# on django-dev, max_num should not prevent existing
|
||||
# related objects/inlines from being displayed.
|
||||
self._queryset = qs
|
||||
return self._queryset
|
||||
|
||||
def save_new(self, form, commit=True):
|
||||
@@ -649,7 +649,7 @@ class BaseModelFormSet(BaseFormSet):
|
||||
def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
|
||||
formset=BaseModelFormSet,
|
||||
extra=1, can_delete=False, can_order=False,
|
||||
max_num=0, fields=None, exclude=None):
|
||||
max_num=None, fields=None, exclude=None):
|
||||
"""
|
||||
Returns a FormSet class for the given Django model class.
|
||||
"""
|
||||
@@ -799,7 +799,7 @@ def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False):
|
||||
def inlineformset_factory(parent_model, model, form=ModelForm,
|
||||
formset=BaseInlineFormSet, fk_name=None,
|
||||
fields=None, exclude=None,
|
||||
extra=3, can_order=False, can_delete=True, max_num=0,
|
||||
extra=3, can_order=False, can_delete=True, max_num=None,
|
||||
formfield_callback=lambda f: f.formfield()):
|
||||
"""
|
||||
Returns an ``InlineFormSet`` for the given kwargs.
|
||||
|
||||
Reference in New Issue
Block a user