Ticket #4622: SelectDateWidget.diff

File SelectDateWidget.diff, 1.6 KB (added by Bill Fenner <fenner@…>, 17 years ago)

A one-line diff and 29 lines of test ;-)

  • django/newforms/extras/widgets.py

     
    5757        y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
    5858        if y and m and d:
    5959            return '%s-%s-%s' % (y, m, d)
    60         return None
     60        return data.get(name, None)
  • tests/regressiontests/forms/tests.py

     
    35363536<option value="2016">2016</option>
    35373537</select>
    35383538
     3539Using a SelectDateWidget in a form:
     3540
     3541>>> class GetDate(Form):
     3542...     mydate = DateField(widget=SelectDateWidget)
     3543>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
     3544>>> print a.is_valid()
     3545True
     3546>>> print a.cleaned_data['mydate']
     35472008-04-01
     3548
     3549As with any widget that implements get_value_from_datadict,
     3550we must be prepared to accept the input from the "as_hidden"
     3551rendering as well.
     3552
     3553>>> print a['mydate'].as_hidden()
     3554<input type="hidden" name="mydate" value="2008-4-1" id="id_mydate" />
     3555>>> b=GetDate({'mydate':'2008-4-1'})
     3556>>> print b.is_valid()
     3557True
     3558>>> print b.cleaned_data['mydate']
     35592008-04-01
     3560
     3561
    35393562# MultiWidget and MultiValueField #############################################
    35403563# MultiWidgets are widgets composed of other widgets. They are usually
    35413564# combined with MultiValueFields - a field that is composed of other fields.
Back to Top