【问题标题】:Odoo 8: onchange many2one not workingOdoo 8:onchange many2one 不起作用
【发布时间】:2016-06-30 09:45:46
【问题描述】:

在accounting->invoice下我试图在从列表中选择一个客户(字段:partner_id:many2one)时触发onchange,但它失败了,而在字段“origin”(类型:char ) 工作正常。有人可以帮忙吗?

注意:在 Odoo 调试模式下,在客户字段上拖动鼠标时显示的帮助消息已绑定到名为 onchange_partner_id(type,...) 的 onchange 函数,我想知道这是否是问题的原因

这里是代码:我从原始发票模型继承而不是添加 onchange 函数

class stock_picking(models.Model):
_inherit = "account.invoice"

#NOT triggered
@api.onchange('partner_id')
def _onchange_customer(self):
    print("debug:y_account_invoice: _onchange_customer:selected")

#triggered successfully    
@api.onchange('origin')
def _onchange_origin(self):
    print("debug:y_account_invoice: _onchange_origin")

【问题讨论】:

    标签: openerp onchange odoo-8 invoice accounting


    【解决方案1】:

    你只需要在py中重写这个方法。

    @api.multi
    def onchange_partner_id(self, type, partner_id, date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
        res = super(classname, self).onchange_partner_id(type, partner_id, date_invoice=date_invoice,payment_term=payment_term, partner_bank_id=partner_bank_id, company_id=company_id)
        #### Update your code 
        # If you want to set any fields value then just simply update it in res and return res
        res['value'].update({'account_id': new_value,})
        return res
    

    onchange_partner_id 已经存在,您需要覆盖它,不要再次定义它。 _onchange_origin 适合您的情况,因为它还没有。

    【讨论】:

    • 感谢 Emipro,这个函数是在 partner_id 更改时触发的,但现在我得到了这个错误:[ ...return new_api(self, *args, **kwargs) TypeError: onchange_partner_id() got multiple values对于关键字参数'date_invoice'。]。请注意,我使用的是 Pyhon 2.7,我还验证了超类中的方法签名,它是 7 参数。该错误在打开新发票时触发(根据默认值解析代码)
    • 你能告诉我你的代码吗?如果您可以将其添加到问题中,那就太好了。
    • 我犯了一个小错误我在方法调用中添加了self,我刚刚从超级方法参数列表中删除了它,现在检查一下。
    • 技术,谢谢。请注意,我是 Python 的初学者,在调用 onchange_partner_id 时从参数列表中排除 self 有点令人困惑,而 self 在此函数的实现中声明。然而,这就是 Python 的工作原理!
    【解决方案2】:

    我找到了我的问题的替代解决方案(不理想)。我已经覆盖了从 account_invoice 核心到从它继承的自定义模块的整个函数,然后在其上添加了我的自定义代码。这样才能正常触发伙伴的change函数。(省略super调用)

      #overwritten function
      @api.multi
      def onchange_partner_id(self, type, partner_id, date_invoice=False,
            payment_term=False, partner_bank_id=False, company_id=False): 
         #KEEP the Core Code
         #custom code
         #add the sales person to the result in case it was not False
         if user_id_sales_per != False:
            print("Debug:account.invoice.onchange_partner_id(): my custom code")
    

    【讨论】:

    • 总是建议调用 super ,因为你永远不知道该方法会从多少个地方被调用,所以尽量避免完全覆盖,除非有必要。因为你认为你已经在你的函数中添加了它的基本代码,但它永远不会向前传递,它会破坏那个执行通道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    相关资源
    最近更新 更多