【问题标题】:Odoo error raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))Odoo 错误引发 ValueError("叶 %r 中的无效字段 %r" % (left, str(leaf)))
【发布时间】:2019-11-26 21:36:05
【问题描述】:

我有一个类 Branch、类 BranchLine、类 Product 和类 TransRequest,其想法是将产品从一个分支转移到另一个分支,我想要做的是当用户选择某个分支时,可用的产品应该只可见类如下

class CustomBranch(models.Model):
    _name = 'custom.branch'
    _description = 'Branch Record'
    _rec_name = 'branch_name'

    branch_name = fields.Char(string="Branch Name", required=False, )
    branch_line = fields.One2many('custom.branch.line', 'branch_id', string='Branch Lines', )


class CustomBranchLine(models.Model):
    _name = 'custom.branch.line'
    _description = 'Branch Line Record'

    branch_id = fields.Many2one('custom.branch', string='Branch')
    product_id = fields.Many2one('custom.product', string='Product')
    qty = fields.Integer(string="QTY", required=False, )


class CustomTransRequest(models.Model):
    _name = 'custom.trans.request'
    _description = 'Transfer Request'

    branch_from_id = fields.Many2one('custom.branch', string="From")
    branch_to_id = fields.Many2one('custom.branch.line', string="To")
    product_ids = fields.Many2many('custom.product', string="Products")

    @api.onchange('branch_from_id')
    def onchange_branch_from(self):
        for rec in self:
            if rec.branch_from_id:
                return {'domain': {'product_ids': [('branch_id', '=', rec.branch_from_id.id)]}}

但是当我选择一个分支时得到这个错误

Error:
Odoo Server Error

Traceback (most recent call last):
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 656, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 314, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/home/odoo/odoo12/odoo/odoo/tools/pycompat.py", line 87, in reraise
    raise value
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 698, in dispatch
    result = self._call_function(**self.params)
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 346, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/odoo/odoo12/odoo/odoo/service/model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 941, in __call__
    return self.method(*args, **kw)
  File "/home/odoo/odoo12/odoo/odoo/http.py", line 519, in response_wrap
    response = f(*args, **kw)
  File "/home/odoo/odoo12/odoo/addons/web/controllers/main.py", line 904, in search_read
    return self.do_search_read(model, fields, offset, limit, domain, sort)
  File "/home/odoo/odoo12/odoo/addons/web/controllers/main.py", line 926, in do_search_read
    offset=offset or 0, limit=limit or False, order=sort or False)
  File "/home/odoo/odoo12/odoo/odoo/models.py", line 4578, in search_read
    records = self.search(domain or [], offset=offset, limit=limit, order=order)
  File "/home/odoo/odoo12/odoo/odoo/models.py", line 1561, in search
    res = self._search(args, offset=offset, limit=limit, order=order, count=count)
  File "/home/odoo/odoo12/odoo/odoo/models.py", line 4110, in _search
    query = self._where_calc(args)
  File "/home/odoo/odoo12/odoo/odoo/models.py", line 3902, in _where_calc
    e = expression.expression(domain, self)
  File "/home/odoo/odoo12/odoo/odoo/osv/expression.py", line 672, in __init__
    self.parse()
  File "/home/odoo/odoo12/odoo/odoo/osv/expression.py", line 853, in parse
    raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
ValueError: Invalid field 'branch_id' in leaf "<osv.ExtendedLeaf: ('branch_id', '=', 3) on custom_product (ctx: )>"

【问题讨论】:

    标签: python odoo-10 odoo-11 odoo-12


    【解决方案1】:

    感谢这篇帖子Odoo: ValueError: Invalid field 'user_id' in leaf 我认为它在 Product 类中寻找字段 ''branch_id',所以我添加了以下字段

    branch_line = fields.One2many('custom.branch.line', 'product_id', string='Branch Lines', )
    

    并将onchange修改为

    @api.onchange('branch_from_id')
        def onchange_branch_from(self):
            for rec in self:
                if rec.branch_from_id:
                    selected_lines = rec.env['custom.branch.line'].search([('branch_id', '=', rec.branch_from_id.id)])
                    for line in selected_lines:
                        return {'domain': {'product_ids': [('branch_line', '=', line.id)]}}
    

    现在的问题是我无法检索超过 1 个产品,有人知道如何将列表发送到域吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2014-11-12
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多