Ticket #9865: inline_custom_pk.diff
File inline_custom_pk.diff, 3.7 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
482 482 return form 483 483 484 484 def save_new(self, form, commit=True): 485 kwargs = {self.fk.get_attname(): self.instance.pk} 485 fk_attname = self.fk.get_attname() 486 kwargs = {fk_attname: self.instance.pk} 486 487 new_obj = self.model(**kwargs) 487 return save_instance(form, new_obj, exclude=[self._pk_field.name], commit=commit) 488 exclude = fk_attname == self._pk_field.attname and [self._pk_field.name] or [] 489 return save_instance(form, new_obj, exclude=exclude, commit=commit) 488 490 489 491 def add_fields(self, form, index): 490 492 super(BaseInlineFormSet, self).add_fields(form, index) -
tests/modeltests/model_formsets/models.py
27 27 28 28 def __unicode__(self): 29 29 return self.title 30 31 class BookWithCustomPK(models.Model): 32 my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True) 33 author = models.ForeignKey(Author) 34 title = models.CharField(max_length=100) 30 35 36 def __unicode__(self): 37 return u'%s: %s' % (self.my_pk, self.title) 38 31 39 class AuthorMeeting(models.Model): 32 40 name = models.CharField(max_length=100) 33 41 authors = models.ManyToManyField(Author) … … 39 47 class CustomPrimaryKey(models.Model): 40 48 my_pk = models.CharField(max_length=10, primary_key=True) 41 49 some_field = models.CharField(max_length=100) 42 43 50 44 51 # models for inheritance tests. 45 52 46 53 class Place(models.Model): … … 484 491 <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> 485 492 <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> 486 493 494 Test inline formsets where the inline-edited object has a custom primary key that is not the fk to the parent object. 495 496 >>> AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1) 497 498 >>> formset = AuthorBooksFormSet2(instance=author) 499 >>> for form in formset.forms: 500 ... print form.as_p() 501 <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> 502 <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> 503 504 >>> data = { 505 ... 'bookwithcustompk_set-TOTAL_FORMS': '1', # the number of forms rendered 506 ... 'bookwithcustompk_set-INITIAL_FORMS': '0', # the number of forms with initial data 507 ... 'bookwithcustompk_set-0-my_pk': '77777', 508 ... 'bookwithcustompk_set-0-title': 'Les Fleurs du Mal', 509 ... } 510 511 >>> formset = AuthorBooksFormSet2(data, instance=author) 512 >>> formset.is_valid() 513 True 514 515 >>> formset.save() 516 [<BookWithCustomPK: 77777: Les Fleurs du Mal>] 517 518 >>> for book in author.bookwithcustompk_set.all(): 519 ... print book.title 520 Les Fleurs du Mal 521 522 487 523 # Test a custom primary key ################################################### 488 524 489 525 We need to ensure that it is displayed