属性错误: 'bool'对象没有属性'strftime'



我正在继承"account.partner.caledger"模块。当我们选择客户时,我们将能够打印客户分类账的报告。在合作伙伴分类账菜单中,如果过滤器按日期/期间,我希望默认选中"包括初始余额"复选框。我试图通过自定义模块覆盖该方法,但无法解决我遇到的错误。

代码,

@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:UserszendynamixodooGitodoo8openerphttp.py", line 544, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:UserszendynamixodooGitodoo8openerphttp.py", line 581, in dispatch
    result = self._call_function(**self.params)
  File "C:UserszendynamixodooGitodoo8openerphttp.py", line 317, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:UserszendynamixodooGitodoo8openerpservicemodel.py", line 118, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:UserszendynamixodooGitodoo8openerphttp.py", line 314, in checked_call
    return self.endpoint(*a, **kw)
  File "C:UserszendynamixodooGitodoo8openerphttp.py", line 810, in __call__
    return self.method(*args, **kw)
  File "C:UserszendynamixodooGitodoo8openerphttp.py", line 410, in response_wrap
    response = f(*args, **kw)
  File "C:UserszendynamixodooGitodoo8addonswebcontrollersmain.py", line 944, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "C:UserszendynamixodooGitodoo8addonswebcontrollersmain.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "C:UserszendynamixodooGitodoo8openerpapi.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "C:UserszendynamixodooGitodoo8openerpapi.py", line 399, in old_api
    result = method(recs, *args, **kwargs)
  File "C:UserszendynamixodooGitodoo8openerpmodels.py", line 5985, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "C:UserszendynamixodooGitodoo8openerpmodels.py", line 5883, in _onchange_eval
    self.update(self._convert_to_cache(method_res['value'], validate=False))
  File "C:UserszendynamixodooGitodoo8openerpmodels.py", line 5391, in _convert_to_cache
    for name, value in values.iteritems()
  File "C:UserszendynamixodooGitodoo8openerpmodels.py", line 5392, in <dictcomp>
    if name in fields
  File "C:UserszendynamixodooGitodoo8openerpfields.py", line 1250, in convert_to_cache
    return self.to_string(value)
  File "C:UserszendynamixodooGitodoo8openerpfields.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

如果值测试的测试条件是查看值是否评估为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)

最新更新