AttributeError: 'bool' object has no attribute 'strftime'

AttributeError: 'bool' object has no attribute 'strftime'

我正在继承 'account.partner.ledger' 模块。当我们 select 客户时,我们将能够打印客户分类帐的报告。在合作伙伴分类帐菜单中,如果过滤器是 date/period,我想默认选中 'include Initial Balances' 复选框。我试图通过我的自定义模块覆盖该方法,但我无法解决我遇到的错误得到.

代码,

@api.multi
def onchange_filter(self,filter='filter_no', fiscalyear_id=False):
    res = super(account_partner_ledger, self).onchange_filter(filter=filter, fiscalyear_id=fiscalyear_id)
    if filter in ['filter_no', 'unreconciled']:
        if filter == 'unreconciled':
            res['value'].update({'fiscalyear_id': False})
        res['value'].update({'initial_balance': False, 'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False})

    if filter in ['filter_date','filter_period']:
        res['value'].update({'initial_balance': True, 'period_from': True, 'period_to': True, 'date_from': True ,'date_to': True})
    return res

错误,

Traceback (most recent call last):
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 544, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 581, in dispatch
    result = self._call_function(**self.params)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 317, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\service\model.py", line 118, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 314, in checked_call
    return self.endpoint(*a, **kw)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 810, in __call__
    return self.method(*args, **kw)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\http.py", line 410, in response_wrap
    response = f(*args, **kw)
  File "C:\Users\zendynamix\odooGit\odoo8\addons\web\controllers\main.py", line 944, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\addons\web\controllers\main.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\api.py", line 399, in old_api
    result = method(recs, *args, **kwargs)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\models.py", line 5985, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\models.py", line 5883, in _onchange_eval
    self.update(self._convert_to_cache(method_res['value'], validate=False))
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\models.py", line 5391, in _convert_to_cache
    for name, value in values.iteritems()
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\models.py", line 5392, in <dictcomp>
    if name in fields
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\fields.py", line 1250, in convert_to_cache
    return self.to_string(value)
  File "C:\Users\zendynamix\odooGit\odoo8\openerp\fields.py", line 1240, in to_string
    return value.strftime(DATE_FORMAT) if value else False
AttributeError: 'bool' object has no attribute 'strftime'

有时您必须查看底层代码以了解发生了什么,您会收到错误,因为 Odoo 正在尝试将布尔对象转换回时间的字符串表示形式(它期望 python 日期对象)

您可以启动终端并重现您的错误:

>>> True.strftime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bool' object has no attribute 'strftime'
>>>

这是来自 odoo

to_string 方法
@staticmethod
def to_string(value):
    """ Convert a :class:`date` value into the format expected by the ORM. """
    return value.strftime(DATE_FORMAT) if value else False

测试条件 if value 测试以查看 value 是否评估为 False,从终端测试

>>> x = ''
>>> if x: print('Yeah')
... 
>>> 
>>> x = True
>>> if x: print('Yeah')
... 
Yeah
>>> x = False
>>> if x: print('Yeah')
... 
>>> 
>>>

从输出中,我们可以得出一个结论,即空字符串或 False 的计算结果为 False,而 True 值的计算结果为 True,因此不要将日期值设置为 True,而是将它们全部设置为空字符串。

@api.multi
def onchange_filter(self,filter='filter_no', fiscalyear_id=False):
    res = super(account_partner_ledger, self).onchange_filter(filter=filter, fiscalyear_id=fiscalyear_id)
    if filter in ['filter_no', 'unreconciled']:
        if filter == 'unreconciled':
            res['value'].update({'fiscalyear_id': False})
        res['value'].update({'initial_balance': False, 'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False})

    if filter in ['filter_date','filter_period']:
        res['value'].update({'initial_balance': 'True', 'period_from': '', 'period_to': '', 'date_from': '', 'date_to': ''})

    return res

当您查看代码时,您会看到:

'date_from': True ,'date_to': True

这会导致您的错误。 您应该将这些字段设置为日期而不是布尔值。 值 False 有效,因为您应该可以不填写日期。

尝试使用 strptime 而不是 strftime,看看是否能解决问题。 您可以使用 strptime,例如:-

from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT my_date = datetime.strptime(self.date_column, DEFAULT_SERVER_DATETIME_FORMAT)