Ticket #10121: form-file-save.diff

File form-file-save.diff, 1.4 KB (added by Alex, 6 years ago)
  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index ce76a14..878d09c 100644
    a b class FileField(Field): 
    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:
     218            setattr(instance, self.name, data)
     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

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index a202185..6ef44d9 100644
    a b True 
    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