Search code examples
pythonodoo-10odoo-11odoo-12

Odoo error raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))


I have a class Branch, class BranchLine, class Product, and class TransRequest, the idea is to make a transfer request of products from one branch to another, what i want to do is when a user selects a certain branch, the available products should only be visible the classes are as follows

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)]}}

but when i select a branch in get this error

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: )>"

Solution

  • Thanks to this post Odoo: ValueError: Invalid field 'user_id' in leaf i figured it was looking for field ''branch_id' in class Product, so i added the following field

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

    and modified the onchange to be

    @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)]}}
    

    the problem now is i can't retrieve more than 1 product, does anyone have an idea how to send a list to domain?