我正在尝试在我的会话中保存日期。我总是收到错误Object of type 'datetime' is not JSON serializable。我发现这这里的Django的文档:stored as seconds since epoch since datetimes are not serializable in JSON.
如何将我的保存expiry_date为秒而不是日期时间?
code = social_ticketing_form.cleaned_data['a']
expiry_date = timezone.now() + timezone.timedelta(days=settings.SOCIAL_TICKETING_ATTRIBUTION_WINDOW)
request.session[social_ticketing_cookie_name(request.event)] = {'code': code, 'expiry_date': expiry_date}
Run Code Online (Sandbox Code Playgroud)
要么编写自己的会话序列化器以允许您datetime直接序列化对象,要么datetime以其他形式存储值。
如果要将其保存为秒,请使用以下datetime.timestamp()方法:
request.session[social_ticketing_cookie_name(request.event)] = {
'code': code,
'expiry_date': expiry_date.timestamp()
}
Run Code Online (Sandbox Code Playgroud)
您自己的SESSION_SERIALIZER类只需要提供loads和dumps方法,直接类似于json.loads()和json.dumps()(这是标准 JSON 序列化器的实现方式)。
如果您想对datetime对象进行编码并能够透明地将它们再次转换回datetime对象,我将使用嵌套对象格式将此类值标记为特殊值:
from datetime import datetime
class JSONDateTimeSerializer:
@staticmethod
def _default(ob):
if isinstance(ob, datetime):
return {'__datetime__': ob.isoformat()}
raise TypeError(type(ob))
@staticmethod
def _object_hook(d):
if '__datetime__' in d:
return datetime.fromisoformat(d['__datetime__'])
return d
def dumps(self, obj):
return json.dumps(
obj, separators=(',', ':'), default=self._default
).encode('latin-1')
def loads(self, data):
return json.loads(
data.decode('latin-1'), object_hook=self._object_hook
)
Run Code Online (Sandbox Code Playgroud)
并设置SESSION_SERIALIZER为上述模块的全限定名 ( path.to.module.JSONDateTimeSerializer)。
以上使用了Python 3.7 中新增的datetime.fromisoformat()方法。