mirror of
				https://github.com/django/django.git
				synced 2025-10-29 08:36:09 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -378,6 +378,7 @@ class BaseModelFormSet(BaseFormSet): | |||||||
|  |  | ||||||
|     def add_fields(self, form, index): |     def add_fields(self, form, index): | ||||||
|         """Add a hidden field for the object's primary key.""" |         """Add a hidden field for the object's primary key.""" | ||||||
|  |         if self.model._meta.has_auto_field: | ||||||
|             self._pk_field_name = self.model._meta.pk.attname |             self._pk_field_name = self.model._meta.pk.attname | ||||||
|             form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) |             form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) | ||||||
|         super(BaseModelFormSet, self).add_fields(form, index) |         super(BaseModelFormSet, self).add_fields(form, index) | ||||||
|   | |||||||
| @@ -29,6 +29,10 @@ class AuthorMeeting(models.Model): | |||||||
|     def __unicode__(self): |     def __unicode__(self): | ||||||
|         return self.name |         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': """ | __test__ = {'API_TESTS': """ | ||||||
|  |  | ||||||
| @@ -330,5 +334,15 @@ Test using a custom prefix on an inline formset. | |||||||
| <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-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> | <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> | ||||||
|  |  | ||||||
| """} | """} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user