summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2008-01-17 18:03:21 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2008-01-17 18:03:21 +0000
commitfd20365b277bf48dbdbd82afa1346eadb96d9574 (patch)
tree1e02d91fca33fb2fd52ff26125fe788470da4e3a /django
parent93c8e94bd6b004ae7a97502e664e6a530958ed9a (diff)
Fixed #6302. FileField no longer requires a value if one already exists. Thanks Brian Rosner and Øyvind Saltvik.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7021 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/db/models/fields/__init__.py6
-rw-r--r--django/newforms/fields.py10
-rw-r--r--django/newforms/forms.py8
3 files changed, 17 insertions, 7 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 139a096b68..f78c81ae39 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -797,13 +797,17 @@ class FileField(Field):
return os.path.normpath(f)
def save_form_data(self, instance, data):
- if data:
+ from django.newforms.fields import UploadedFile
+ if data and isinstance(data, UploadedFile):
getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False)
def formfield(self, **kwargs):
defaults = {'form_class': forms.FileField}
# If a file has been provided previously, then the form doesn't require
# that a new file is provided this time.
+ # The code to mark the form field as not required is used by
+ # form_for_instance, but can probably be removed once form_for_instance
+ # is gone. ModelForm uses a different method to check for an existing file.
if 'initial' in kwargs:
defaults['required'] = False
defaults.update(kwargs)
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index 3b8f4195b0..0761b8f2af 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -437,10 +437,12 @@ class FileField(Field):
def __init__(self, *args, **kwargs):
super(FileField, self).__init__(*args, **kwargs)
- def clean(self, data):
- super(FileField, self).clean(data)
+ def clean(self, data, initial=None):
+ super(FileField, self).clean(initial or data)
if not self.required and data in EMPTY_VALUES:
return None
+ elif not data and initial:
+ return initial
try:
f = UploadedFile(data['filename'], data['content'])
except TypeError:
@@ -456,12 +458,12 @@ class ImageField(FileField):
'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."),
}
- def clean(self, data):
+ def clean(self, data, initial=None):
"""
Checks that the file-upload field data contains a valid image (GIF, JPG,
PNG, possibly others -- whatever the Python Imaging Library supports).
"""
- f = super(ImageField, self).clean(data)
+ f = super(ImageField, self).clean(data, initial)
if f is None:
return None
from PIL import Image
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 556c00a777..04bf62e445 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -9,7 +9,7 @@ from django.utils.html import escape
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
from django.utils.safestring import mark_safe
-from fields import Field
+from fields import Field, FileField
from widgets import TextInput, Textarea
from util import flatatt, ErrorDict, ErrorList, ValidationError
@@ -182,7 +182,11 @@ class BaseForm(StrAndUnicode):
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
- value = field.clean(value)
+ if isinstance(field, FileField):
+ initial = self.initial.get(name, field.initial)
+ value = field.clean(value, initial)
+ else:
+ value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()