diff options
Diffstat (limited to 'tests/model_forms')
| -rw-r--r-- | tests/model_forms/models.py | 16 | ||||
| -rw-r--r-- | tests/model_forms/tests.py | 32 |
2 files changed, 47 insertions, 1 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index 59b18dc39d..8936a17fc0 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -406,3 +406,19 @@ class Character(models.Model): class StumpJoke(models.Model): most_recently_fooled = models.ForeignKey(Character, limit_choices_to=today_callable_dict, related_name="+") has_fooled_today = models.ManyToManyField(Character, limit_choices_to=today_callable_q, related_name="+") + + +# Model for #639 +class Photo(models.Model): + title = models.CharField(max_length=30) + image = models.FileField(storage=temp_storage, upload_to='tests') + + # Support code for the tests; this keeps track of how many times save() + # gets called on each instance. + def __init__(self, *args, **kwargs): + super(Photo, self).__init__(*args, **kwargs) + self._savecount = 0 + + def save(self, force_insert=False, force_update=False): + super(Photo, self).save(force_insert, force_update) + self._savecount += 1 diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index dc912c0cd6..f05307851f 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -22,7 +22,7 @@ from django.utils import six from .models import (Article, ArticleStatus, Author, Author1, BetterWriter, BigInt, Book, Category, CommaSeparatedInteger, CustomFF, CustomFieldForExclusionModel, DerivedBook, DerivedPost, Document, ExplicitPK, FilePathModel, FlexibleDatePost, Homepage, - ImprovedArticle, ImprovedArticleWithParentLink, Inventory, Person, Post, Price, + ImprovedArticle, ImprovedArticleWithParentLink, Inventory, Person, Photo, Post, Price, Product, Publication, TextFile, Triple, Writer, WriterProfile, Colour, ColourfulItem, ArticleStatusNote, DateTimePost, CustomErrorMessage, test_images, StumpJoke, Character) @@ -1838,6 +1838,36 @@ class FileAndImageFieldTests(TestCase): form = CFFForm(data={'f': None}) form.save() + def test_file_field_multiple_save(self): + """ + Simulate a file upload and check how many times Model.save() gets + called. Test for bug #639. + """ + class PhotoForm(forms.ModelForm): + class Meta: + model = Photo + fields = '__all__' + + # Grab an image for testing. + filename = os.path.join(os.path.dirname(upath(__file__)), "test.png") + with open(filename, "rb") as fp: + img = fp.read() + + # Fake a POST QueryDict and FILES MultiValueDict. + data = {'title': 'Testing'} + files = {"image": SimpleUploadedFile('test.png', img, 'image/png')} + + form = PhotoForm(data=data, files=files) + p = form.save() + + try: + # Check the savecount stored on the object (see the model). + self.assertEqual(p._savecount, 1) + finally: + # Delete the "uploaded" file to avoid clogging /tmp. + p = Photo.objects.get() + p.image.delete(save=False) + def test_file_path_field_blank(self): """ Regression test for #8842: FilePathField(blank=True) |
