diff options
| author | Karen Tracey <kmtracey@gmail.com> | 2008-11-04 19:54:28 +0000 |
|---|---|---|
| committer | Karen Tracey <kmtracey@gmail.com> | 2008-11-04 19:54:28 +0000 |
| commit | 8e4827e5068ebe26e8669ffa676b7d6764fbb00d (patch) | |
| tree | 120f161a12c20776e5064487ba8614c5897aecdf /tests/modeltests | |
| parent | 4563060093ad6d30c76b62dce4d822f9426eaef2 (diff) | |
[1.0.X] Fixed #9418 -- When saving a model form, defer saving of file-type fields until after other fields, so that callable upload_to methods can use data from the other fields. Thanks to Bernd Schlapsi for the report and initial patch.
[9334] from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9335 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests')
| -rw-r--r-- | tests/modeltests/model_forms/models.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 0df21b6aa2..01728a997e 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -99,6 +99,10 @@ class TextFile(models.Model): return self.description class ImageFile(models.Model): + def custom_upload_path(self, filename): + path = self.path or 'tests' + return '%s/%s' % (path, filename) + description = models.CharField(max_length=20) try: # If PIL is available, try testing PIL. @@ -106,9 +110,10 @@ class ImageFile(models.Model): # for PyPy, you need to check for the underlying modules # If PIL is not available, this test is equivalent to TextFile above. from PIL import Image, _imaging - image = models.ImageField(storage=temp_storage, upload_to='tests') + image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path) except ImportError: - image = models.FileField(storage=temp_storage, upload_to='tests') + image = models.FileField(storage=temp_storage, upload_to=custom_upload_path) + path = models.CharField(max_length=16, blank=True, default='') def __unicode__(self): return self.description @@ -1122,6 +1127,15 @@ True <...FieldFile: tests/test3.png> >>> instance.delete() +# Test callable upload_to behavior that's dependent on the value of another field in the model +>>> f = ImageFileForm(data={'description': u'And a final one', 'path': 'foo'}, files={'image': SimpleUploadedFile('test4.png', image_data)}) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.image +<...FieldFile: foo/test4.png> +>>> instance.delete() + # Media on a ModelForm ######################################################## # Similar to a regular Form class you can define custom media to be used on |
