AttributeError:' bool'对象没有属性' strftime'

时间:2016-08-08 06:34:25

标签: openerp odoo-8

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

代码,

@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'

3 个答案:

答案 0 :(得分:1)

你有时需要查看底层代码才能理解发生了什么,因为Odoo试图将一个布尔对象转换回一个时间的字符串表示(它需要一个python日期对象),所以你会收到错误。 p>

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

>>> 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

答案 1 :(得分:1)

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

'date_from': True ,'date_to': True

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

答案 2 :(得分:0)

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

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