Skip to content

Commit

Permalink
Reverted [7986].
Browse files Browse the repository at this point in the history
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
malcolmt committed Jul 19, 2008
1 parent a461994 commit 5141b67
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
4 changes: 2 additions & 2 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ def _get_next_or_previous_in_order(self, is_next):

def _get_FIELD_filename(self, field):
if getattr(self, field.attname): # Value is not blank.
return os.path.normpath(os.path.join(settings.MEDIA_ROOT, field.get_filename(getattr(self, field.attname))))
return os.path.normpath(os.path.join(settings.MEDIA_ROOT, getattr(self, field.attname)))
return ''

def _get_FIELD_url(self, field):
if getattr(self, field.attname): # Value is not blank.
import urlparse
return urlparse.urljoin(settings.MEDIA_URL, field.get_filename(getattr(self, field.attname))).replace('\\', '/')
return urlparse.urljoin(settings.MEDIA_URL, getattr(self, field.attname)).replace('\\', '/')
return ''

def _get_FIELD_size(self, field):
Expand Down
26 changes: 3 additions & 23 deletions tests/modeltests/model_forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

from django.db import models

TEMP_DIR = tempfile.gettempdir()

ARTICLE_STATUS = (
(1, 'Draft'),
(2, 'Pending'),
Expand Down Expand Up @@ -62,7 +60,7 @@ def __unicode__(self):

class TextFile(models.Model):
description = models.CharField(max_length=20)
file = models.FileField(upload_to=TEMP_DIR)
file = models.FileField(upload_to=tempfile.gettempdir())

def __unicode__(self):
return self.description
Expand All @@ -73,9 +71,9 @@ class ImageFile(models.Model):
# If PIL is available, try testing PIL.
# Otherwise, it's equivalent to TextFile above.
import Image
image = models.ImageField(upload_to=TEMP_DIR)
image = models.ImageField(upload_to=tempfile.gettempdir())
except ImportError:
image = models.FileField(upload_to=TEMP_DIR)
image = models.FileField(upload_to=tempfile.gettempdir())

def __unicode__(self):
return self.description
Expand Down Expand Up @@ -786,24 +784,6 @@ def __unicode__(self):
# FileField ###################################################################
# File instance methods. Tests fix for #5619.
>>> instance = TextFile(description='nothing', file='name')
>>> expected = '%s/name' % TEMP_DIR
>>> instance.get_file_filename() == expected
True
>>> instance.get_file_url() == expected
True
>>> instance.save_file_file(instance.file, SimpleUploadedFile(instance.file, 'some text'))
>>> instance.get_file_filename() == expected
True
>>> instance.get_file_url() == expected
True
>>> os.unlink(instance.get_file_filename())
# File forms.
>>> class TextFileForm(ModelForm):
... class Meta:
... model = TextFile
Expand Down

0 comments on commit 5141b67

Please sign in to comment.