odoo - NameError 名称未定义

odoo - NameError name is not defined

我正在尝试根据在核心 sale.py 文件中完成的方式创建一个字段。但它给了我

NameError: name '_amount_all_wrapper' is not defined.

我的sale.py代码:

class SaleOrder(osv.Model):
_inherit = 'sale.order'

_columns = {
    'xx_delivery_date': fields.date(string='Delivery date'),
    #'xx_payment_method': fields.selection([('visa', 'Visa'),
    #                                       ('cash', 'Cash')],
    #                                      string='Payment method'),
    'xx_payment_method': fields.many2one('xx.payment.method',
                                         string='Payment method'),
    'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance'),
    #test to move line
    'amount_insurance': fields.function(_amount_all_wrapper, digits_compute=dp.get_precision('Account'), string='Insurance',
        store={
            'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
            'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
        },
        multi='sums', help="Amount untaxed plus insurance percentage."),
}

def _amount_all_wrapper(self, cr, uid, ids, field_name, arg, context=None):
    """ Wrapper because of direct method passing as parameter for function fields """
    return self._amount_all(cr, uid, ids, field_name, arg, context=context)

错误指的是'amount_insurance': fields.function(_amount_all_wrapper, digits_compute=dp.get_precision('Account'), string='Insurance',

由于这与他们所做的完全相同,而且除了方法之外,我在他们的代码中找不到任何对 _amount_all_wrapper 的引用,所以我真的没有弄错。

_amount_all_wrapper 的定义需要在您开始引用它之前存在。添加 self 将不起作用,因为在执行这行代码时对象尚未创建。在解释器加载脚本并创建所有 class 定义时,您甚至会在执行任何代码之前收到此错误。因此有必要在实际引用之前定义对象。

所以只需像这样移动代码:

class SaleOrder(osv.Model):
    _inherit = 'sale.order'

    def _amount_all_wrapper(self, cr, uid, ids, field_name, arg, context=None):
        """ Wrapper because of direct method passing as parameter for function fields """
        return self._amount_all(cr, uid, ids, field_name, arg, context=context)

    _columns = {
        'xx_delivery_date': fields.date(string='Delivery date'),
        #'xx_payment_method': fields.selection([('visa', 'Visa'),
        #                                       ('cash', 'Cash')],
        #                                      string='Payment method'),
        'xx_payment_method': fields.many2one('xx.payment.method',
                                             string='Payment method'),
        'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance'),
        #test to move line
        'amount_insurance': fields.function(_amount_all_wrapper, digits_compute=dp.get_precision('Account'), string='Insurance',
            store={
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
            },
            multi='sums', help="Amount untaxed plus insurance percentage."),
    }