Django

Code

Changeset 7998

Show
Ignore:
Timestamp:
07/19/08 17:26:32 (2 months ago)
Author:
mtredinnick
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r7986 r7998  
    460460    def _get_FIELD_filename(self, field): 
    461461        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))) 
    463463        return '' 
    464464 
     
    466466        if getattr(self, field.attname): # Value is not blank. 
    467467            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('\\', '/') 
    469469        return '' 
    470470 
  • django/trunk/tests/modeltests/model_forms/models.py

    r7986 r7998  
    1212 
    1313from django.db import models 
    14  
    15 TEMP_DIR = tempfile.gettempdir() 
    1614 
    1715ARTICLE_STATUS = ( 
     
    6361class TextFile(models.Model): 
    6462    description = models.CharField(max_length=20) 
    65     file = models.FileField(upload_to=TEMP_DIR
     63    file = models.FileField(upload_to=tempfile.gettempdir()
    6664 
    6765    def __unicode__(self): 
     
    7472        # Otherwise, it's equivalent to TextFile above. 
    7573        import Image 
    76         image = models.ImageField(upload_to=TEMP_DIR
     74        image = models.ImageField(upload_to=tempfile.gettempdir()
    7775    except ImportError: 
    78         image = models.FileField(upload_to=TEMP_DIR
     76        image = models.FileField(upload_to=tempfile.gettempdir()
    7977 
    8078    def __unicode__(self): 
     
    787785# FileField ################################################################### 
    788786 
    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  
    807787>>> class TextFileForm(ModelForm): 
    808788...     class Meta: