Ticket #9865: inline_custom_pk.diff

File inline_custom_pk.diff, 3.7 KB (added by Karen Tracey, 16 years ago)
  • django/forms/models.py

     
    482482        return form
    483483   
    484484    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}
    486487        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)
    488490
    489491    def add_fields(self, form, index):
    490492        super(BaseInlineFormSet, self).add_fields(form, index)
  • tests/modeltests/model_formsets/models.py

     
    2727
    2828    def __unicode__(self):
    2929        return self.title
     30   
     31class 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)
    3035
     36    def __unicode__(self):
     37        return u'%s: %s' % (self.my_pk, self.title)
     38   
    3139class AuthorMeeting(models.Model):
    3240    name = models.CharField(max_length=100)
    3341    authors = models.ManyToManyField(Author)
     
    3947class CustomPrimaryKey(models.Model):
    4048    my_pk = models.CharField(max_length=10, primary_key=True)
    4149    some_field = models.CharField(max_length=100)
    42 
    43 
     50   
    4451# models for inheritance tests.
    4552
    4653class Place(models.Model):
     
    484491<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>
    485492<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>
    486493
     494Test 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()
     513True
     514
     515>>> formset.save()
     516[<BookWithCustomPK: 77777: Les Fleurs du Mal>]
     517
     518>>> for book in author.bookwithcustompk_set.all():
     519...     print book.title
     520Les Fleurs du Mal
     521
     522
    487523# Test a custom primary key ###################################################
    488524
    489525We need to ensure that it is displayed
Back to Top