Python Openerp 7-raise TypeError(repr(o)&“x2B”不可JSON序列化)

Python Openerp 7-raise TypeError(repr(o)&“x2B”不可JSON序列化),python,python-2.7,odoo,openerp-7,Python,Python 2.7,Odoo,Openerp 7,我试图使用一个函数将默认值设置为我的datetime字段之一,该函数同时返回一个datetime值 问题是每当我试图创建一个新记录(错误发布点)时,它都会给我这个错误消息,我不知道如何解决这个问题。请帮我做这个 错误 功能 def _getLastFromTime(self, cr, uid, context=None): tsht_ids=self.search(cr, uid, [('user_id', '=', uid)], context=context) tsht_

我试图使用一个函数将默认值设置为我的datetime字段之一,该函数同时返回一个datetime值

问题是每当我试图创建一个新记录(错误发布点)时,它都会给我这个错误消息,我不知道如何解决这个问题。请帮我做这个

错误

功能

def _getLastFromTime(self, cr, uid, context=None):

    tsht_ids=self.search(cr, uid, [('user_id', '=', uid)], context=context)
    tsht_strt_tme=datetime.datetime.now()
    if tsht_ids:
        last_id=max(tsht_ids)
        tsht_time=self.browse(cr, uid, tsht_ids[0], context=context).date_to
        tsht_strt_tme=datetime.datetime.strptime(tsht_time, '%Y-%m-%d %H:%M:%S')

    else:
        return False     

    return tsht_strt_tme

函数必须以Odoo需要的格式返回字符串

fields.Date
fields.Datetime
中有一些很好的方法可以将python日期(time)转换为与Odoo兼容的字符串,或者以其他方式(Odoo 8+)转换

还有其他一些方法

Date.today()
-以utc格式将今天作为与Odoo日期字段兼容的字符串返回

Date.context\u today()
-以与Odoo日期兼容的字符串形式返回用户时区中的今天

Date.from_string()
-将Odoo日期字符串转换为python日期对象

Date.to\u string()
-从\u string

模拟日期方法:
Datetime.now()
Datetime.context\u timestamp()
Datetime.from\u string()
Datetime.to\u string()

      'date_from':fields.datetime(string='Date From'),
      'date_to':fields.datetime(string='Date To'),

 _defaults = {_getLastFromTime}
def _getLastFromTime(self, cr, uid, context=None):

    tsht_ids=self.search(cr, uid, [('user_id', '=', uid)], context=context)
    tsht_strt_tme=datetime.datetime.now()
    if tsht_ids:
        last_id=max(tsht_ids)
        tsht_time=self.browse(cr, uid, tsht_ids[0], context=context).date_to
        tsht_strt_tme=datetime.datetime.strptime(tsht_time, '%Y-%m-%d %H:%M:%S')

    else:
        return False     

    return tsht_strt_tme