Ticket #7975: callable_initial_fix_r8640.diff
File callable_initial_fix_r8640.diff, 6.8 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/__init__.py
231 231 232 232 def get_default(self): 233 233 "Returns the default value for this field." 234 if self. default is not NOT_PROVIDED:234 if self.has_default(): 235 235 if callable(self.default): 236 236 return self.default() 237 237 return force_unicode(self.default, strings_only=True) … … 308 308 defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs))) 309 309 if self.has_default(): 310 310 defaults['initial'] = self.get_default() 311 if callable(self.default): 312 defaults['show_hidden_initial'] = True 311 313 defaults.update(kwargs) 312 314 return form_class(**defaults) 313 315 -
django/forms/fields.py
27 27 from django.utils.encoding import smart_unicode, smart_str 28 28 29 29 from util import ErrorList, ValidationError 30 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput 30 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput, SplitHiddenDateTimeWidget 31 31 from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile 32 32 33 33 __all__ = ( … … 57 57 creation_counter = 0 58 58 59 59 def __init__(self, required=True, widget=None, label=None, initial=None, 60 help_text=None, error_messages=None ):60 help_text=None, error_messages=None, show_hidden_initial=False): 61 61 # required -- Boolean that specifies whether the field is required. 62 62 # True by default. 63 63 # widget -- A Widget class, or instance of a Widget class, that should … … 71 71 # initial -- A value to use in this Field's initial display. This value 72 72 # is *not* used as a fallback if data isn't given. 73 73 # help_text -- An optional string to use as "help text" for this Field. 74 # show_hidden_initial -- Boolean that specifies if is needed to render a 75 # hidden widget with initial value after widget. 74 76 if label is not None: 75 77 label = smart_unicode(label) 76 78 self.required, self.label, self.initial = required, label, initial 79 self.show_hidden_initial = show_hidden_initial 77 80 if help_text is None: 78 81 self.help_text = u'' 79 82 else: … … 800 803 self.widget.choices = self.choices 801 804 802 805 class SplitDateTimeField(MultiValueField): 806 hidden_widget = SplitHiddenDateTimeWidget 803 807 default_error_messages = { 804 808 'invalid_date': _(u'Enter a valid date.'), 805 809 'invalid_time': _(u'Enter a valid time.'), -
django/forms/forms.py
128 128 """ 129 129 return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name 130 130 131 def add_initial_prefix(self, field_name): 132 """ 133 Add a 'initial' prefix for checking dynamic initial values 134 """ 135 return u'initial-%s' % self.add_prefix(field_name) 136 131 137 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): 132 138 "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 133 139 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. … … 245 251 Returns True if data differs from initial. 246 252 """ 247 253 return bool(self.changed_data) 248 254 249 255 def _get_changed_data(self): 250 256 if self._changed_data is None: 251 257 self._changed_data = [] … … 258 264 for name, field in self.fields.items(): 259 265 prefixed_name = self.add_prefix(name) 260 266 data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) 261 initial_value = self.initial.get(name, field.initial) 267 if not field.show_hidden_initial: 268 initial_value = self.initial.get(name, field.initial) 269 else: 270 initial_prefixed_name = self.add_initial_prefix(name) 271 raw_initial_value = field.hidden_widget().value_from_datadict(self.data, 272 self.files, initial_prefixed_name) 273 initial_value = field.clean(raw_initial_value) 262 274 if field.widget._has_changed(initial_value, data_value): 263 275 self._changed_data.append(name) 264 276 return self._changed_data … … 300 312 self.field = field 301 313 self.name = name 302 314 self.html_name = form.add_prefix(name) 315 self.html_check_name = form.add_initial_prefix(name) 303 316 if self.field.label is None: 304 317 self.label = pretty_name(name) 305 318 else: … … 336 349 data = data() 337 350 else: 338 351 data = self.data 339 return widget.render(self.html_name, data, attrs=attrs) 352 field_html = widget.render(self.html_name, data, attrs=attrs) 353 if self.field.show_hidden_initial: 354 field_html += self.field.hidden_widget().render(self.html_check_name, data) 355 return field_html 340 356 341 357 def as_text(self, attrs=None): 342 358 """ -
django/forms/widgets.py
25 25 'HiddenInput', 'MultipleHiddenInput', 26 26 'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput', 27 27 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 28 'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget', 28 'CheckboxSelectMultiple', 'MultiWidget', 29 'SplitDateTimeWidget', 'SplitHiddenDateTimeWidget', 29 30 ) 30 31 31 32 MEDIA_TYPES = ('css','js') … … 662 663 return [value.date(), value.time().replace(microsecond=0)] 663 664 return [None, None] 664 665 666 class SplitHiddenDateTimeWidget(SplitDateTimeWidget): 667 """ 668 A Widget that splits datetime input into two <input type="text"> boxes. 669 """ 670 def __init__(self, attrs=None): 671 widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs)) 672 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 673 674