Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个表示事件开始时间的字段,为此我使用了 Django DateTimeField。此字段是强制性的,但有时用户只会知道开始日期而不知道时间。有没有办法让时间部分成为可选的,并保持日期部分是强制性的?
也许您应该尝试将日期与时间分开。有 DateField 和 TimeField 。
用于视图或模型的示例:
您可以使用函数strptime以任何格式显示日期时间字段。
from datetime import datetime datetime.now().strftime('%Y-%m-%d') # print string '2013-06-25'
在模板中使用的示例:
你可以使用模板标签日期
{{ datetime_field|date:"Ymd" }}