Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure that custom primary keys are always shown on a formset. Refs #…

…7938. Thanks magneto for discovering this problem.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8179 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit af35fb1fbb48e6125b313ad35238cd06fa3ce7b5 1 parent 61b3357
@brosner brosner authored
View
5 django/forms/models.py
@@ -378,8 +378,9 @@ def save_new_objects(self, commit=True):
def add_fields(self, form, index):
"""Add a hidden field for the object's primary key."""
- self._pk_field_name = self.model._meta.pk.attname
- form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
+ if self.model._meta.has_auto_field:
+ self._pk_field_name = self.model._meta.pk.attname
+ form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
super(BaseModelFormSet, self).add_fields(form, index)
def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
View
14 tests/modeltests/model_formsets/models.py
@@ -29,6 +29,10 @@ class AuthorMeeting(models.Model):
def __unicode__(self):
return self.name
+class CustomPrimaryKey(models.Model):
+ my_pk = models.CharField(max_length=10, primary_key=True)
+ some_field = models.CharField(max_length=100)
+
__test__ = {'API_TESTS': """
@@ -330,5 +334,15 @@ def __unicode__(self):
<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>
<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>
+# Test a custom primary key ###################################################
+
+We need to ensure that it is displayed
+
+>>> CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey)
+>>> formset = CustomPrimaryKeyFormSet()
+>>> for form in formset.forms:
+... print form.as_p()
+<p><label for="id_form-0-my_pk">My pk:</label> <input id="id_form-0-my_pk" type="text" name="form-0-my_pk" maxlength="10" /></p>
+<p><label for="id_form-0-some_field">Some field:</label> <input id="id_form-0-some_field" type="text" name="form-0-some_field" maxlength="100" /></p>
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.