Ticket #13811: formset_validation_tests.diff

File formset_validation_tests.diff, 3.6 KB (added by Claude Paroz, 14 years ago)

Complementary test for this issue

  • tests/modeltests/model_formsets/models.py

     
    3535    def __unicode__(self):
    3636        return u'%s: %s' % (self.my_pk, self.title)
    3737
     38class Editor(models.Model):
     39    name = models.CharField(max_length=100)
     40
     41class BookWithOptionalAltEditor(models.Model):
     42    author = models.ForeignKey(Author)
     43    # Optional secondary author
     44    alt_editor = models.ForeignKey(Editor, blank=True, null=True)
     45    title = models.CharField(max_length=100)
     46
     47    class Meta:
     48        unique_together = (
     49            ('author', 'title', 'alt_editor'),
     50        )
     51
     52    def __unicode__(self):
     53        return self.title
     54
    3855class AlternateBook(Book):
    3956    notes = models.CharField(max_length=100)
    4057
     
    648665>>> formset.save()
    649666[<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
    650667
     668Test inline formsets where the inline-edited object has a unique_together constraint with a nullable member
    651669
     670>>> AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2)
     671
     672>>> formset = AuthorBooksFormSet4(instance=author)
     673>>> for form in formset.forms:
     674...     print form.as_p()
     675<p><label for="id_bookwithoptionalalteditor_set-0-alt_editor">Alt editor:</label> <select name="bookwithoptionalalteditor_set-0-alt_editor" id="id_bookwithoptionalalteditor_set-0-alt_editor">
     676    <option value="" selected="selected">---------</option>
     677    </select></p>
     678    <p><label for="id_bookwithoptionalalteditor_set-0-title">Title:</label> <input id="id_bookwithoptionalalteditor_set-0-title" type="text" name="bookwithoptionalalteditor_set-0-title" maxlength="100" /><input type="hidden" name="bookwithoptionalalteditor_set-0-author" value="1" id="id_bookwithoptionalalteditor_set-0-author" /><input type="hidden" name="bookwithoptionalalteditor_set-0-id" id="id_bookwithoptionalalteditor_set-0-id" /></p>
     679    <p><label for="id_bookwithoptionalalteditor_set-1-alt_editor">Alt editor:</label> <select name="bookwithoptionalalteditor_set-1-alt_editor" id="id_bookwithoptionalalteditor_set-1-alt_editor">
     680    <option value="" selected="selected">---------</option>
     681    </select></p>
     682    <p><label for="id_bookwithoptionalalteditor_set-1-title">Title:</label> <input id="id_bookwithoptionalalteditor_set-1-title" type="text" name="bookwithoptionalalteditor_set-1-title" maxlength="100" /><input type="hidden" name="bookwithoptionalalteditor_set-1-author" value="1" id="id_bookwithoptionalalteditor_set-1-author" /><input type="hidden" name="bookwithoptionalalteditor_set-1-id" id="id_bookwithoptionalalteditor_set-1-id" /></p>
     683
     684>>> data = {
     685...     'bookwithoptionalalteditor_set-TOTAL_FORMS': '2', # the number of forms rendered
     686...     'bookwithoptionalalteditor_set-INITIAL_FORMS': '0', # the number of forms with initial data
     687...     'bookwithoptionalalteditor_set-MAX_NUM_FORMS': '', # the max number of forms
     688...     'bookwithoptionalalteditor_set-0-author': '1',
     689...     'bookwithoptionalalteditor_set-0-title': 'Les Fleurs du Mal',
     690...     'bookwithoptionalalteditor_set-1-author': '1',
     691...     'bookwithoptionalalteditor_set-1-title': 'Les Fleurs du Mal',
     692... }
     693
     694>>> formset = AuthorBooksFormSet4(data, instance=author)
     695>>> formset.is_valid()
     696True
     697
     698>>> formset.save()
     699[<BookWithOptionalAltEditor: Les Fleurs du Mal>, <BookWithOptionalAltEditor: Les Fleurs du Mal>]
     700
     701
    652702# ModelForm with a custom save method in an inline formset ###################
    653703
    654704>>> class PoemForm(forms.ModelForm):
Back to Top