Ticket #7975: callable_initial_fix_r8640.2.diff

File callable_initial_fix_r8640.2.diff, 6.8 KB (added by Manuel Saelices, 16 years ago)

Only a typo in a doc string

  • django/db/models/fields/__init__.py

     
    231231
    232232    def get_default(self):
    233233        "Returns the default value for this field."
    234         if self.default is not NOT_PROVIDED:
     234        if self.has_default():
    235235            if callable(self.default):
    236236                return self.default()
    237237            return force_unicode(self.default, strings_only=True)
     
    308308            defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs)))
    309309        if self.has_default():
    310310            defaults['initial'] = self.get_default()
     311            if callable(self.default):
     312                defaults['show_hidden_initial'] = True
    311313        defaults.update(kwargs)
    312314        return form_class(**defaults)
    313315
  • django/forms/fields.py

     
    2727from django.utils.encoding import smart_unicode, smart_str
    2828
    2929from util import ErrorList, ValidationError
    30 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput
     30from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput, SplitHiddenDateTimeWidget
    3131from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
    3232
    3333__all__ = (
     
    5757    creation_counter = 0
    5858
    5959    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):
    6161        # required -- Boolean that specifies whether the field is required.
    6262        #             True by default.
    6363        # widget -- A Widget class, or instance of a Widget class, that should
     
    7171        # initial -- A value to use in this Field's initial display. This value
    7272        #            is *not* used as a fallback if data isn't given.
    7373        # 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.
    7476        if label is not None:
    7577            label = smart_unicode(label)
    7678        self.required, self.label, self.initial = required, label, initial
     79        self.show_hidden_initial = show_hidden_initial
    7780        if help_text is None:
    7881            self.help_text = u''
    7982        else:
     
    800803        self.widget.choices = self.choices
    801804
    802805class SplitDateTimeField(MultiValueField):
     806    hidden_widget = SplitHiddenDateTimeWidget
    803807    default_error_messages = {
    804808        'invalid_date': _(u'Enter a valid date.'),
    805809        'invalid_time': _(u'Enter a valid time.'),
  • django/forms/forms.py

     
    128128        """
    129129        return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
    130130
     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
    131137    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
    132138        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    133139        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
     
    245251        Returns True if data differs from initial.
    246252        """
    247253        return bool(self.changed_data)
    248    
     254
    249255    def _get_changed_data(self):
    250256        if self._changed_data is None:
    251257            self._changed_data = []
     
    258264            for name, field in self.fields.items():
    259265                prefixed_name = self.add_prefix(name)
    260266                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)
    262274                if field.widget._has_changed(initial_value, data_value):
    263275                    self._changed_data.append(name)
    264276        return self._changed_data
     
    300312        self.field = field
    301313        self.name = name
    302314        self.html_name = form.add_prefix(name)
     315        self.html_check_name = form.add_initial_prefix(name)
    303316        if self.field.label is None:
    304317            self.label = pretty_name(name)
    305318        else:
     
    336349                data = data()
    337350        else:
    338351            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
    340356
    341357    def as_text(self, attrs=None):
    342358        """
  • django/forms/widgets.py

     
    2525    'HiddenInput', 'MultipleHiddenInput',
    2626    'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
    2727    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
    28     'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
     28    'CheckboxSelectMultiple', 'MultiWidget',
     29    'SplitDateTimeWidget', 'SplitHiddenDateTimeWidget',
    2930)
    3031
    3132MEDIA_TYPES = ('css','js')
     
    662663            return [value.date(), value.time().replace(microsecond=0)]
    663664        return [None, None]
    664665
     666class SplitHiddenDateTimeWidget(SplitDateTimeWidget):
     667    """
     668    A Widget that splits datetime input into two <input type="hidden"> inputs.
     669    """
     670    def __init__(self, attrs=None):
     671        widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs))
     672        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
     673
     674
Back to Top