Django

Code

Changeset 9334

Show
Ignore:
Timestamp:
11/04/08 13:48:35 (8 months ago)
Author:
kmtracey
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r9263 r9334  
    345345    Vinay Sajip <vinay_sajip@yahoo.co.uk> 
    346346    David Schein 
     347    Bernd Schlapsi 
    347348    scott@staplefish.com 
    348349    Ilya Semenov <semenov@inetss.com> 
  • django/trunk/django/forms/models.py

    r9326 r9334  
    4242                         " validate." % (opts.object_name, fail_message)) 
    4343    cleaned_data = form.cleaned_data 
     44    file_field_list = [] 
    4445    for f in opts.fields: 
    4546        if not f.editable or isinstance(f, models.AutoField) \ 
     
    5051        if exclude and f.name in exclude: 
    5152            continue 
     53        # Defer saving file-type fields until after the other fields, so a 
     54        # callable upload_to can use the values from other fields. 
     55        if isinstance(f, models.FileField): 
     56            file_field_list.append(f) 
     57        else: 
     58            f.save_form_data(instance, cleaned_data[f.name]) 
     59             
     60    for f in file_field_list: 
    5261        f.save_form_data(instance, cleaned_data[f.name]) 
     62         
    5363    # Wrap up the saving of m2m data as a function. 
    5464    def save_m2m(): 
  • django/trunk/tests/modeltests/model_forms/models.py

    r9288 r9334  
    100100 
    101101class ImageFile(models.Model): 
     102    def custom_upload_path(self, filename): 
     103        path = self.path or 'tests' 
     104        return '%s/%s' % (path, filename) 
     105     
    102106    description = models.CharField(max_length=20) 
    103107    try: 
     
    107111        # If PIL is not available, this test is equivalent to TextFile above. 
    108112        from PIL import Image, _imaging 
    109         image = models.ImageField(storage=temp_storage, upload_to='tests'
     113        image = models.ImageField(storage=temp_storage, upload_to=custom_upload_path
    110114    except ImportError: 
    111         image = models.FileField(storage=temp_storage, upload_to='tests') 
     115        image = models.FileField(storage=temp_storage, upload_to=custom_upload_path) 
     116    path = models.CharField(max_length=16, blank=True, default='') 
    112117 
    113118    def __unicode__(self): 
     
    11231128>>> instance.delete() 
    11241129 
     1130# Test callable upload_to behavior that's dependent on the value of another field in the model 
     1131>>> f = ImageFileForm(data={'description': u'And a final one', 'path': 'foo'}, files={'image': SimpleUploadedFile('test4.png', image_data)}) 
     1132>>> f.is_valid() 
     1133True 
     1134>>> instance = f.save() 
     1135>>> instance.image 
     1136<...FieldFile: foo/test4.png> 
     1137>>> instance.delete() 
     1138 
    11251139# Media on a ModelForm ######################################################## 
    11261140