From ca33f07e2c555c16b60ae287ffa994c7a3d6c1c9 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sun, 21 Dec 2008 04:56:32 +0000 Subject: [PATCH] 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 --- django/forms/models.py | 9 ++++-- 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.

+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() +

+

+ +>>> 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() +[] + +>>> 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