diff options
| author | Karen Tracey <kmtracey@gmail.com> | 2008-12-21 04:56:32 +0000 |
|---|---|---|
| committer | Karen Tracey <kmtracey@gmail.com> | 2008-12-21 04:56:32 +0000 |
| commit | ca33f07e2c555c16b60ae287ffa994c7a3d6c1c9 (patch) | |
| tree | b4f139937793d2eef0aa04c56c4c9a8b767982b1 | |
| parent | 04ed4234690c43f6cd294f1cfb8a978e13378903 (diff) | |
Fixed #9865 -- Allow saving of new inline-edited objects with custom non-auto primary key fields that are not the foreign key to the parent object.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9664 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/forms/models.py | 9 | ||||
| -rw-r--r-- | tests/modeltests/model_formsets/models.py | 36 |
2 files changed, 43 insertions, 2 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index 81a7439331..d8c038faad 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -482,9 +482,14 @@ class BaseInlineFormSet(BaseModelFormSet): return form def save_new(self, form, commit=True): - kwargs = {self.fk.get_attname(): self.instance.pk} + fk_attname = self.fk.get_attname() + kwargs = {fk_attname: self.instance.pk} new_obj = self.model(**kwargs) - return save_instance(form, new_obj, exclude=[self._pk_field.name], commit=commit) + if fk_attname == self._pk_field.attname: + exclude = [self._pk_field.name] + else: + exclude = [] + return save_instance(form, new_obj, exclude=exclude, commit=commit) def add_fields(self, form, index): super(BaseInlineFormSet, self).add_fields(form, index) diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py index 8e28e6fccd..f11d4538ef 100644 --- a/tests/modeltests/model_formsets/models.py +++ b/tests/modeltests/model_formsets/models.py @@ -27,7 +27,15 @@ class Book(models.Model): def __unicode__(self): return self.title + +class BookWithCustomPK(models.Model): + my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True) + author = models.ForeignKey(Author) + title = models.CharField(max_length=100) + def __unicode__(self): + return u'%s: %s' % (self.my_pk, self.title) + class AuthorMeeting(models.Model): name = models.CharField(max_length=100) authors = models.ManyToManyField(Author) @@ -484,6 +492,34 @@ 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-author" id="id_test-0-author" /><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-author" id="id_test-1-author" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p> +Test inline formsets where the inline-edited object has a custom primary key that is not the fk to the parent object. + +>>> AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1) + +>>> formset = AuthorBooksFormSet2(instance=author) +>>> for form in formset.forms: +... print form.as_p() +<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input type="text" name="bookwithcustompk_set-0-my_pk" id="id_bookwithcustompk_set-0-my_pk" /></p> + <p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p> + +>>> data = { +... 'bookwithcustompk_set-TOTAL_FORMS': '1', # the number of forms rendered +... 'bookwithcustompk_set-INITIAL_FORMS': '0', # the number of forms with initial data +... 'bookwithcustompk_set-0-my_pk': '77777', +... 'bookwithcustompk_set-0-title': 'Les Fleurs du Mal', +... } + +>>> formset = AuthorBooksFormSet2(data, instance=author) +>>> formset.is_valid() +True + +>>> formset.save() +[<BookWithCustomPK: 77777: Les Fleurs du Mal>] + +>>> for book in author.bookwithcustompk_set.all(): +... print book.title +Les Fleurs du Mal + # Test a custom primary key ################################################### We need to ensure that it is displayed |
