Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Reverted [7986].

It turns out that we need to treat filename creation/display (in
particular, the upload_to path) differently depending upon whether the value is
out of the database or provided by other code and there's no reliable way to
determine that at the moment (although some later proposed changes might alter
that). So calling get_FIELD_filename on an unsaved model with a changed file
attribute will not necessarily return the same result as after the save().

Refs #5619. Fixed #7843.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7998 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5141b67a193a4e460460827f4d6859a31319aba5 1 parent a461994
Malcolm Tredinnick authored July 19, 2008
4  django/db/models/base.py
@@ -459,13 +459,13 @@ def _get_next_or_previous_in_order(self, is_next):
459 459
 
460 460
     def _get_FIELD_filename(self, field):
461 461
         if getattr(self, field.attname): # Value is not blank.
462  
-            return os.path.normpath(os.path.join(settings.MEDIA_ROOT, field.get_filename(getattr(self, field.attname))))
  462
+            return os.path.normpath(os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)))
463 463
         return ''
464 464
 
465 465
     def _get_FIELD_url(self, field):
466 466
         if getattr(self, field.attname): # Value is not blank.
467 467
             import urlparse
468  
-            return urlparse.urljoin(settings.MEDIA_URL, field.get_filename(getattr(self, field.attname))).replace('\\', '/')
  468
+            return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
469 469
         return ''
470 470
 
471 471
     def _get_FIELD_size(self, field):
26  tests/modeltests/model_forms/models.py
@@ -12,8 +12,6 @@
12 12
 
13 13
 from django.db import models
14 14
 
15  
-TEMP_DIR = tempfile.gettempdir()
16  
-
17 15
 ARTICLE_STATUS = (
18 16
     (1, 'Draft'),
19 17
     (2, 'Pending'),
@@ -62,7 +60,7 @@ def __unicode__(self):
62 60
 
63 61
 class TextFile(models.Model):
64 62
     description = models.CharField(max_length=20)
65  
-    file = models.FileField(upload_to=TEMP_DIR)
  63
+    file = models.FileField(upload_to=tempfile.gettempdir())
66 64
 
67 65
     def __unicode__(self):
68 66
         return self.description
@@ -73,9 +71,9 @@ class ImageFile(models.Model):
73 71
         # If PIL is available, try testing PIL.
74 72
         # Otherwise, it's equivalent to TextFile above.
75 73
         import Image
76  
-        image = models.ImageField(upload_to=TEMP_DIR)
  74
+        image = models.ImageField(upload_to=tempfile.gettempdir())
77 75
     except ImportError:
78  
-        image = models.FileField(upload_to=TEMP_DIR)
  76
+        image = models.FileField(upload_to=tempfile.gettempdir())
79 77
 
80 78
     def __unicode__(self):
81 79
         return self.description
@@ -786,24 +784,6 @@ def __unicode__(self):
786 784
 
787 785
 # FileField ###################################################################
788 786
 
789  
-# File instance methods. Tests fix for #5619.
790  
-
791  
->>> instance = TextFile(description='nothing', file='name')
792  
->>> expected = '%s/name' % TEMP_DIR
793  
->>> instance.get_file_filename() == expected
794  
-True
795  
->>> instance.get_file_url() == expected
796  
-True
797  
->>> instance.save_file_file(instance.file, SimpleUploadedFile(instance.file, 'some text'))
798  
->>> instance.get_file_filename() == expected
799  
-True
800  
->>> instance.get_file_url() == expected
801  
-True
802  
-
803  
->>> os.unlink(instance.get_file_filename())
804  
-
805  
-# File forms.
806  
-
807 787
 >>> class TextFileForm(ModelForm):
808 788
 ...     class Meta:
809 789
 ...         model = TextFile

0 notes on commit 5141b67

Please sign in to comment.
Something went wrong with that request. Please try again.