diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index ce76a14a7c..878d09c976 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -213,6 +213,10 @@ class FileField(Field): def generate_filename(self, instance, filename): return os.path.join(self.get_directory_name(), self.get_filename(filename)) + def save_form_data(self, instance, data): + if data: + setattr(instance, self.name, data) + def formfield(self, **kwargs): defaults = {'form_class': forms.FileField} # If a file has been provided previously, then the form doesn't require diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index a202185ec6..6ef44d909b 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -1030,6 +1030,18 @@ True >>> instance.file +# Instance can be edited w/out re-uploading the file and existing file should be preserved. + +>>> f = TextFileForm(data={'description': u'New Description'}, instance=instance) +>>> f.fields['file'].required = False +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.description +u'New Description' +>>> instance.file + + # Delete the current file since this is not done by Django. >>> instance.file.delete() >>> instance.delete()