Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed 10075: Allowed saving of inline-edited models that use multi-ta…

…ble inheritance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 81ae2afdec8a81b94bacf02a1242277e0b4caceb 1 parent ecadf67
Karen Tracey authored February 03, 2009
2  django/forms/models.py
@@ -493,7 +493,7 @@ def save_new(self, form, commit=True):
493 493
         fk_attname = self.fk.get_attname()
494 494
         kwargs = {fk_attname: self.instance.pk}
495 495
         new_obj = self.model(**kwargs)
496  
-        if fk_attname == self._pk_field.attname:
  496
+        if fk_attname == self._pk_field.attname or self._pk_field.auto_created:
497 497
             exclude =  [self._pk_field.name]
498 498
         else:
499 499
             exclude = []
33  tests/modeltests/model_formsets/models.py
@@ -36,6 +36,12 @@ class BookWithCustomPK(models.Model):
36 36
     def __unicode__(self):
37 37
         return u'%s: %s' % (self.my_pk, self.title)
38 38
     
  39
+class AlternateBook(Book):
  40
+    notes = models.CharField(max_length=100)
  41
+    
  42
+    def __unicode__(self):
  43
+        return u'%s - %s' % (self.title, self.notes)
  44
+    
39 45
 class AuthorMeeting(models.Model):
40 46
     name = models.CharField(max_length=100)
41 47
     authors = models.ManyToManyField(Author)
@@ -520,6 +526,33 @@ def __unicode__(self):
520 526
 ...     print book.title
521 527
 Les Fleurs du Mal
522 528
 
  529
+Test inline formsets where the inline-edited object uses multi-table inheritance, thus 
  530
+has a non AutoField yet auto-created primary key.
  531
+
  532
+>>> AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1)
  533
+
  534
+>>> formset = AuthorBooksFormSet3(instance=author)
  535
+>>> for form in formset.forms:
  536
+...     print form.as_p()
  537
+<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>
  538
+    <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>
  539
+
  540
+
  541
+>>> data = {
  542
+...     'alternatebook_set-TOTAL_FORMS': '1', # the number of forms rendered
  543
+...     'alternatebook_set-INITIAL_FORMS': '0', # the number of forms with initial data
  544
+...     'alternatebook_set-0-title': 'Flowers of Evil',
  545
+...     'alternatebook_set-0-notes': 'English translation of Les Fleurs du Mal'
  546
+... }
  547
+
  548
+>>> formset = AuthorBooksFormSet3(data, instance=author)
  549
+>>> formset.is_valid()
  550
+True
  551
+
  552
+>>> formset.save()
  553
+[<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
  554
+
  555
+
523 556
 # Test a custom primary key ###################################################
524 557
 
525 558
 We need to ensure that it is displayed

0 notes on commit 81ae2af

Please sign in to comment.
Something went wrong with that request. Please try again.