Skip to content

Commit

Permalink
Fixed 10075: Allowed saving of inline-edited models that use multi-ta…
Browse files Browse the repository at this point in the history
…ble inheritance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
kmtracey committed Feb 3, 2009
1 parent ecadf67 commit 81ae2af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/forms/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def save_new(self, form, commit=True):
fk_attname = self.fk.get_attname() fk_attname = self.fk.get_attname()
kwargs = {fk_attname: self.instance.pk} kwargs = {fk_attname: self.instance.pk}
new_obj = self.model(**kwargs) new_obj = self.model(**kwargs)
if fk_attname == self._pk_field.attname: if fk_attname == self._pk_field.attname or self._pk_field.auto_created:
exclude = [self._pk_field.name] exclude = [self._pk_field.name]
else: else:
exclude = [] exclude = []
Expand Down
33 changes: 33 additions & 0 deletions tests/modeltests/model_formsets/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class BookWithCustomPK(models.Model):
def __unicode__(self): def __unicode__(self):
return u'%s: %s' % (self.my_pk, self.title) return u'%s: %s' % (self.my_pk, self.title)


class AlternateBook(Book):
notes = models.CharField(max_length=100)

def __unicode__(self):
return u'%s - %s' % (self.title, self.notes)

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 @@ -520,6 +526,33 @@ def __unicode__(self):
... print book.title ... print book.title
Les Fleurs du Mal Les Fleurs du Mal
Test inline formsets where the inline-edited object uses multi-table inheritance, thus
has a non AutoField yet auto-created primary key.
>>> AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1)
>>> formset = AuthorBooksFormSet3(instance=author)
>>> for form in formset.forms:
... print form.as_p()
<p><label for="id_alternatebook_set-0-title">Title:</label> <input id="id_alternatebook_set-0-title" type="text" name="alternatebook_set-0-title" maxlength="100" /></p>
<p><label for="id_alternatebook_set-0-notes">Notes:</label> <input id="id_alternatebook_set-0-notes" type="text" name="alternatebook_set-0-notes" maxlength="100" /><input type="hidden" name="alternatebook_set-0-author" value="1" id="id_alternatebook_set-0-author" /><input type="hidden" name="alternatebook_set-0-book_ptr" id="id_alternatebook_set-0-book_ptr" /></p>
>>> data = {
... 'alternatebook_set-TOTAL_FORMS': '1', # the number of forms rendered
... 'alternatebook_set-INITIAL_FORMS': '0', # the number of forms with initial data
... 'alternatebook_set-0-title': 'Flowers of Evil',
... 'alternatebook_set-0-notes': 'English translation of Les Fleurs du Mal'
... }
>>> formset = AuthorBooksFormSet3(data, instance=author)
>>> formset.is_valid()
True
>>> formset.save()
[<AlternateBook: Flowers of Evil - English translation of 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 81ae2af

Please sign in to comment.