Ticket #6302: pass_initial_through_messy_example.diff
File pass_initial_through_messy_example.diff, 4.4 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
13 13 from django.core import validators 14 14 from django import oldforms 15 15 from django import newforms as forms 16 from django.newforms.utils import InitialData 16 17 from django.core.exceptions import ObjectDoesNotExist 17 18 from django.utils.functional import curry 18 19 from django.utils.itercompat import tee … … 795 796 return os.path.normpath(f) 796 797 797 798 def save_form_data(self, instance, data): 798 if data :799 if data and not isinstance(data, InitialData): 799 800 getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False) 800 801 801 802 def formfield(self, **kwargs): -
django/newforms/util.py
67 67 # AttributeError: ValidationError instance has no attribute 'args' 68 68 # See http://www.python.org/doc/current/tut/node10.html#handling 69 69 return repr(self.messages) 70 71 class InitialData(object): 72 73 value = None 74 75 def __init__(self, value): 76 self.value = value -
django/newforms/fields.py
19 19 from django.utils.translation import ugettext_lazy as _ 20 20 from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str 21 21 22 from util import ErrorList, ValidationError 22 from util import ErrorList, ValidationError, InitialData 23 23 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput 24 24 25 25 … … 441 441 super(FileField, self).clean(data) 442 442 if not self.required and data in EMPTY_VALUES: 443 443 return None 444 elif self.required and isinstance(data, InitialData): 445 return data 444 446 try: 445 447 f = UploadedFile(data['filename'], data['content']) 446 448 except TypeError: -
django/newforms/forms.py
9 9 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode 10 10 from django.utils.safestring import mark_safe 11 11 12 from fields import Field 12 from fields import Field, EMPTY_VALUES 13 13 from widgets import Media, media_property, TextInput, Textarea 14 14 from util import flatatt, ErrorDict, ErrorList, ValidationError 15 15 … … 204 204 # Each widget type knows how to retrieve its own data, because some 205 205 # widgets split data over several HTML fields. 206 206 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 207 if value in EMPTY_VALUES: 208 value = field.widget.value_from_initial(self.initial, name, value) 207 209 try: 208 210 value = field.clean(value) 209 211 self.cleaned_data[name] = value -
django/newforms/widgets.py
15 15 from django.utils.translation import ugettext 16 16 from django.utils.encoding import StrAndUnicode, force_unicode 17 17 from django.utils.safestring import mark_safe 18 from util import flatatt 18 from util import flatatt, InitialData 19 19 from urlparse import urljoin 20 20 21 21 __all__ = ( … … 202 202 final_attrs['value'] = force_unicode(value) 203 203 return mark_safe(u'<input%s />' % flatatt(final_attrs)) 204 204 205 def value_from_initial(self, initial, name, data): 206 return data 207 205 208 class TextInput(Input): 206 209 input_type = 'text' 207 210 … … 253 256 "File widgets take data from FILES, not POST" 254 257 return files.get(name, None) 255 258 259 def value_from_initial(self, initial, name, data): 260 if initial.has_key(name): 261 return InitialData(initial.get(name)) 262 else: 263 return data 264 256 265 class Textarea(Widget): 257 266 def __init__(self, attrs=None): 258 267 # The 'rows' and 'cols' attributes are required for HTML correctness.