diff --git a/django/forms/models.py b/django/forms/models.py index 134746f4fc..1f807f2b42 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -378,8 +378,9 @@ class BaseModelFormSet(BaseFormSet): 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(), diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index b685d728d3..5b66d1560b 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/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 @@ Test using a custom prefix on an inline formset.
+# 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() +
+
"""}