2017-09-20 40 views
0

我有一個接受請求正文中的start_time,end_time和布爾closed_all_day的api。使用瓶的API記錄的具體時間格式restplus

from flask_restplus import Namespace, fields 

timings = api.model('open times', { 
    'start_time': fields.String(required=True, description='Time in 24 hour HH:MM format, defaulted to 00:00 if closed_all_day'), 
    'end_time': fields.String(required=True, description='Time in 24 hour HH:MM format, defaulted to 00:00 if closed_all_day'), 
    'closed_all_day': fields.Boolean(required=True, description='If True overwrites start_time and end_time') 
}) 

START_TIME和END_TIME的格式將是HH:MM(24小時制)

如果我使用

fields.Date 

fields.DateTime 

然後我得到了完整的ISO日期格式,這也不是我想要的。

有沒有辦法將輸入限制爲HH:MM格式?

回答

1

這是爲了做到這一點:

from datetime import time 


class TimeFormat(fields.Raw): 
    def format(self, value): 
     return time.strftime(value, "%H:%M") 

timings = Model('timings', { 
    'start_time': TimeFormat(readonly=True, description='Time in HH:MM', default='HH:MM'), 
    'end_time': TimeFormat(readonly=True, description='Time in HH:MM', default='HH:MM'), 
    'closed_all_day': fields.Boolean(readOnly=True, description='True or False', default=False) 
})