Thanks for that clear explanation, and your solution works great! Adrian
On Jun 24, 9:23 pm, Karen Tracey <kmtra...@gmail.com> wrote: > On Wed, Jun 24, 2009 at 3:57 PM, adrian <adrian...@gmail.com> wrote: > > > I need a time field that can be None. > > > I define the field so that Null is legal in the DB: > > > time_end = models.TimeField(blank=True, null=True) > > > The form field is: > > > time_end = forms.ChoiceField(required=False, choices=END_TIME_CHOICES) > > > and END_TIME_CHOICES is: > > > END_TIME_CHOICES = ( > > (None, 'End Time Unknown'), > > (datetime.time(0,0), '12:00 Midnight'), > > (datetime.time(0,30), '12:30 AM'), > > (datetime.time(1,0), '1:00 AM'), > > (datetime.time(1,30), '1:30 AM'), > > etc, > > ) > > > My question is why does this field give a validation error when None > > is selected, saying: > > > ValidationError at /event/add_event/ > > > Enter a valid time in HH:MM[:ss[.uuuuuu]] format. > > The problem is that your empty choice None becomes the string "None" when it > is rendered as an HTML select input element, and comes back in the POST data > as a the string "None", not Python's None, so the model TimeField tries to > parse the string value "None" as a time and fails. First way that comes to > mind to fix it is to change your None to an empty string in > END_TIME_CHOICES, and use a TypedChoiceField with empty_value set to None > for your form field: > > time_end = forms.TypedChoiceField(required=False, > choices=END_TIME_CHOICES, empty_value=None) > > Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---