Python Odoo:域不使用相关字段

Python Odoo:域不使用相关字段,python,odoo,Python,Odoo,我有一个模型“custom.products”,它通过一个one2many字段“branch_line”连接到模型“custom.branch.line”,我想在相关字段“branch1”和“branch2”上应用域过滤器,我想它显示每个分支中每个产品的数量,但当我将域应用到相关字段时,它不起作用,它只是检索数据库表中的第一个条目,下面是我的代码 class CustomProduct(models.Model): _name = 'custom.product' _descri

我有一个模型“custom.products”,它通过一个one2many字段“branch_line”连接到模型“custom.branch.line”,我想在相关字段“branch1”和“branch2”上应用域过滤器,我想它显示每个分支中每个产品的数量,但当我将域应用到相关字段时,它不起作用,它只是检索数据库表中的第一个条目,下面是我的代码

class CustomProduct(models.Model):
    _name = 'custom.product'
    _description = 'Product Record'

    branch_line = fields.One2many('custom.branch.line', 'product_id', string='Branch Lines', )
    branch1 = fields.Integer(string="branch 1", related="branch_line.qty", domain="[('branch_line.branch_id','=', 3)]", )
    branch2 = fields.Integer(string="branch 2", related="branch_line.qty", domain="[('branch_line.branch_id','=', 4)]", )

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

您应该使用computed field来获取branch1和branch2字段的正确值:

branch1 = fields.Integer(string="branch 1",compute="_get_branch")
branch2 = fields.Integer(string="branch 2",compute="_get_branch")

@api.multi
def _get_branch(self):
    for line in self:
        line.branch1 = self.env['custom.branch.line'].search([('branch_id','=',3)],limit=1).qty
        line.branch2 =         self.env['custom.branch.line'].search([('branch_id','=',4)],limit=1).qty