summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2010-01-26 15:02:53 +0000
committerJannis Leidel <jannis@leidel.info>2010-01-26 15:02:53 +0000
commitc14937cf7a1e8c25702e89485cc2dd33aa0d3a16 (patch)
tree41894e28b3b2ba2e5c0c8adfea919f2553835644 /django/forms
parentdf82175c17667b76ac968e059583f795b8909526 (diff)
Fixed #12508 - Added ability to dynamically add inlines in the admin app.
Refs #13. Also introduces an ``empty_form`` attribute on formsets to make it easier to implement dynamic forms. Many thanks to Zain Memon for the initial patch from his Summer of Code 2009 project, Stanislaus Madueke for his django-dynamic-formset app and all the other people helping out. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12297 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/formsets.py17
-rw-r--r--django/forms/models.py5
2 files changed, 20 insertions, 2 deletions
diff --git a/django/forms/formsets.py b/django/forms/formsets.py
index d5101c762e..a86c18f138 100644
--- a/django/forms/formsets.py
+++ b/django/forms/formsets.py
@@ -119,6 +119,21 @@ class BaseFormSet(StrAndUnicode):
return self.forms[self.initial_form_count():]
extra_forms = property(_get_extra_forms)
+ def _get_empty_form(self, **kwargs):
+ defaults = {
+ 'auto_id': self.auto_id,
+ 'prefix': self.add_prefix('__prefix__'),
+ 'empty_permitted': True,
+ }
+ if self.data or self.files:
+ defaults['data'] = self.data
+ defaults['files'] = self.files
+ defaults.update(kwargs)
+ form = self.form(**defaults)
+ self.add_fields(form, None)
+ return form
+ empty_form = property(_get_empty_form)
+
# Maybe this should just go away?
def _get_cleaned_data(self):
"""
@@ -268,7 +283,7 @@ class BaseFormSet(StrAndUnicode):
"""A hook for adding extra fields on to each form instance."""
if self.can_order:
# Only pre-fill the ordering field for initial forms.
- if index < self.initial_form_count():
+ if index is not None and index < self.initial_form_count():
form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_(u'Order'), initial=index+1, required=False)
else:
form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_(u'Order'), required=False)
diff --git a/django/forms/models.py b/django/forms/models.py
index ec28b446bd..f343a1cbcd 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -620,7 +620,10 @@ class BaseModelFormSet(BaseFormSet):
pk_value = form.instance.pk
else:
try:
- pk_value = self.get_queryset()[index].pk
+ if index is not None:
+ pk_value = self.get_queryset()[index].pk
+ else:
+ pk_value = None
except IndexError:
pk_value = None
if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey):