Ticket #10075: 10075.diff

File 10075.diff, 2.7 KB (added by kmtracey, 6 years ago)
  • django/forms/models.py

     
    493493        fk_attname = self.fk.get_attname()
    494494        kwargs = {fk_attname: self.instance.pk}
    495495        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:
    497497            exclude =  [self._pk_field.name]
    498498        else:
    499499            exclude = []
  • tests/modeltests/model_formsets/models.py

     
    3636    def __unicode__(self):
    3737        return u'%s: %s' % (self.my_pk, self.title)
    3838   
     39class AlternateBook(Book):
     40    notes = models.CharField(max_length=100)
     41   
     42    def __unicode__(self):
     43        return u'%s - %s' % (self.title, self.notes)
     44   
    3945class AuthorMeeting(models.Model):
    4046    name = models.CharField(max_length=100)
    4147    authors = models.ManyToManyField(Author)
     
    520526...     print book.title
    521527Les Fleurs du Mal
    522528
     529Test inline formsets where the inline-edited object uses multi-table inheritance, thus
     530has 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()
     550True
     551
     552>>> formset.save()
     553[<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
     554
     555
    523556# Test a custom primary key ###################################################
    524557
    525558We need to ensure that it is displayed
Back to Top