Skip to content

Commit

Permalink
[1.0.X] Fixed #9865 -- Allow saving of new inline-edited objects with…
Browse files Browse the repository at this point in the history
… custom non-auto primary key fields that are not the foreign key to the parent object.

r9664 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9665 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
kmtracey committed Dec 21, 2008
1 parent 26554aa commit 73534cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 7 additions & 2 deletions django/forms/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -482,9 +482,14 @@ def _construct_form(self, i, **kwargs):
return form return form


def save_new(self, form, commit=True): 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) 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): def add_fields(self, form, index):
super(BaseInlineFormSet, self).add_fields(form, index) super(BaseInlineFormSet, self).add_fields(form, index)
Expand Down
36 changes: 36 additions & 0 deletions tests/modeltests/model_formsets/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ class Book(models.Model):


def __unicode__(self): def __unicode__(self):
return self.title 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): class AuthorMeeting(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author) authors = models.ManyToManyField(Author)
Expand Down Expand Up @@ -484,6 +492,34 @@ 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-author" id="id_test-0-author" /><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-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> <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 ################################################### # Test a custom primary key ###################################################
We need to ensure that it is displayed We need to ensure that it is displayed
Expand Down

0 comments on commit 73534cd

Please sign in to comment.