SteveC
SteveC

Reputation: 153

Django DateTimeField with optional time part

I have a field which will represent the start time of an event and I am using the Django DateTimeField for this. This field is mandatory but sometimes the users will only know the start date and not the time. Is there anyway to make the time part optional and keep the date part mandatory?

Upvotes: 4

Views: 1604

Answers (2)

Abbasov Alexander
Abbasov Alexander

Reputation: 1938

Example for use at the views or models:

You can use function strptime to show the datetime field any formats.

from datetime import datetime
datetime.now().strftime('%Y-%m-%d')
# print string '2013-06-25'

Example for use at the templates:

you can use templatetag date

{{ datetime_field|date:"Y-m-d" }}

Upvotes: 0

Dmitrii Mikhailov
Dmitrii Mikhailov

Reputation: 5221

Maybe you should try to separate date from time. There are DateField and TimeField for that.

Upvotes: 2

Related Questions