TimeField with minute-resolution in forms

51 views
Skip to first unread message

Brian

unread,
Jul 25, 2011, 8:02:17 PM7/25/11
to Django users
Hi,

I have a form with a TimeField and I'd like it to only accept values
in the form hh:mm, as opposed to hh:mm:ss.

The form is a ModelForm and I've overridden the 'time' field with a
TimeField where I've specified the input_formats as ['%H:%M']. This
works fine when entering value for a new instance of the model, but
when I edit an existing instance, the value that I entered as '12:30'
is rendered as '12:30:00' and so the instance fails to save.

Without much hope of success (and that proved wildly optimistic), I
tried changing the rendering of the form as follows:
{% if field.label == "Time" %}
{{ field | time:"H:i" }}
{% else %}
{{ field }}
{%endif%}
But that didn't work even a little bit.

Do I need to subclass TimeField? Is there a method that controls the
"output_formats"?
Or maybe that's not the way to do it. Could anyone offer any advice?

Thanks in advance,

Brian

Andre Terra

unread,
Jul 25, 2011, 8:16:16 PM7/25/11
to django...@googlegroups.com
I'm mostly guessing, but you can try defining a custom widget for the
field. I'm not sure how much of the behavior of datetime fields
depends on the javascript that gets slapped to them. So instead, maybe
you could override the field's to_python method to append/replace the
seconds as needed, or basically always setting them to 00. Now that I
think of it, this is probably the easiest solution.

Cheers,
AT

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Brian

unread,
Jul 25, 2011, 8:30:55 PM7/25/11
to django...@googlegroups.com
Is that not the opposite problem? I enter a value, which is correctly rejected if I specify a non-zero seconds value, but when I try to edit it again, the value in the form's entry-box has been rendered as "hh:mm:00". Surely its the "from_python" method that I need to override - if such a thing exists? 

Brian

Brian

unread,
Jul 28, 2011, 4:16:39 PM7/28/11
to django...@googlegroups.com
Just in case anyone else has the same problem and finds their way here...

The answers were there in the documentation once I rooted around for a while. I just need to override the default form field provided for my time model field and explicitly specify the allowed formats.

class EntryForm(ModelForm):
    time = TimeField(required=True,
                     error_messages = {'invalid' :
                                       "Please enter a time of the form HH:MM"},
                     widget=TimeInput(format='%H:%M'),
                     input_formats=['%H:%M'])
    class Meta:
        model = Entry
        # time is a models.TimeField in entry/models.py

Reply all
Reply to author
Forward
0 new messages