我正在为Flask项目使用Marshmallow 2.15.3,并想强制使用严格的Date和DateTime格式。严格来说,我的意思是我只想接受与以下格式相同的字符串。我遇到的是在处理Date和DateTime方面的一些区别。格式:
DATE_FORMAT = '%Y-%m-%d'
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
例如,使用DateTime,我可以执行以下操作:
dt = fields.DateTime(format=DATETIME_FORMAT)
dt.deserialize('2018-01-01') # fails, as desired
dt.deserialize('2018-01-01T05:06:08.012312+02:00') # fails, as desired
dt.deserialize('2018-01-01T05:06:08') # works, as desired and according to format
使用日期,我可以执行以下操作:
d = fields.Date() # does not accept format argument
d.deserialize('2018-01') # fails, as desired
d.deserialize('2018-01-01T05:06:08.012312+02:00') # works, NOT as desired
d.deserialize('2018-01-01') # works, as desired and according to format
虽然DateTime不允许额外的信息,但是Date允许它。据我了解,“日期”字段没有format
参数。我有什么办法可以解决类似功能的问题,并对太短和太长的输入值都强制执行严格的格式?
答案 0 :(得分:0)
对于面向未来的代码,我发现棉花糖3.0.0b17中的Date
类现在是DateTime
的子类,而不是Field
,使它继承了format
kwarg(relevant commit)。
对于版本2.15.3和(通常为2.X.X),我无法找到任何内置函数。一种解决方法是猴子修补fields.Date
类。经过修改,看起来像这样:
class Date(Field):
"""ISO8601-formatted date string.
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""
default_error_messages = {
'invalid': 'Not a valid date.',
'format': '"{input}" cannot be formatted as a date.',
}
def __init__(self, format=None, **kwargs):
super(Date, self).__init__(**kwargs)
self.dateformat = format
def _serialize(self, value, attr, obj):
if value is None:
return None
try:
return value.isoformat()
except AttributeError:
self.fail('format', input=value)
return value
def _deserialize(self, value, attr, data):
"""Deserialize an ISO8601-formatted date string to a
:class:`datetime.date` object.
"""
if not value: # falsy values are invalid
self.fail('invalid')
elif self.dateformat:
try:
return dt.datetime.strptime(value, self.dateformat).date()
except (TypeError, AttributeError, ValueError):
raise self.fail('invalid')
try:
return utils.from_iso_date(value)
except (AttributeError, TypeError, ValueError):
self.fail('invalid')
此处的修改是添加了__init__
定义,并在_deserialize
下添加了整个elif self.dateformat
子句。这使我可以使用提交的格式反序列化,例如:
d = fields.Date('%Y-%m-%d') # now accepts a format
d.deserialize('2018-01-01T05:06:08.012312+02:00') # fails, as desired