Ticket #10121: 10121.diff

File 10121.diff, 1.5 KB (added by Karen Tracey, 16 years ago)
  • django/db/models/fields/files.py

     
    213213    def generate_filename(self, instance, filename):
    214214        return os.path.join(self.get_directory_name(), self.get_filename(filename))
    215215
     216    def save_form_data(self, instance, data):
     217        if data and isinstance(data, UploadedFile):
     218            getattr(instance, self.name).save(data.name, data, save=False)
     219
    216220    def formfield(self, **kwargs):
    217221        defaults = {'form_class': forms.FileField}
    218222        # If a file has been provided previously, then the form doesn't require
  • tests/modeltests/model_forms/models.py

     
    10301030>>> instance.file
    10311031<FieldFile: tests/test3.txt>
    10321032
     1033# Instance can be edited w/out re-uploading the file and existing file should be preserved.
     1034
     1035>>> f = TextFileForm(data={'description': u'New Description'}, instance=instance)
     1036>>> f.fields['file'].required = False
     1037>>> f.is_valid()
     1038True
     1039>>> instance = f.save()
     1040>>> instance.description
     1041u'New Description'
     1042>>> instance.file
     1043<FieldFile: tests/test3.txt>
     1044
    10331045# Delete the current file since this is not done by Django.
    10341046>>> instance.file.delete()
    10351047>>> instance.delete()
Back to Top