为什么与X模型相关的Many2one字段显示Y模型的选项?

Why a Many2one field related to X model is showing options of Y model?

我的自定义模型中有以下字段,名称为 accounting_heads.elementary_head:

sub_heads = fields.Many2one('accounting_heads.sub_heads',domain = "[('heads', '=', heads)]", string= 'Sub Heads')

尽管与 accounting_heads.sub_headsMany2one 关系 模型,它显示 accounting_heads.accounting_heads 模型的记录。

为什么?

这是我的模型:

from odoo import models, fields, api

class accounting_heads(models.Model):
    _name = 'accounting_heads.accounting_heads'
    _rec_name = 'heads'

    heads = fields.Char()


class sub_heads(models.Model):
    _name = 'accounting_heads.sub_heads'
    _inherit = 'accounting_heads.accounting_heads'

    heads = fields.Many2one('accounting_heads.accounting_heads', string= 'Heads')
    sub_heads= fields.Char(string ='Sub Heads')


class elementary_heads(models.Model):
    _name = 'accounting_heads.elementary_head'

    _inherits = {'accounting_heads.accounting_heads': 'heads',
     'accounting_heads.sub_heads' : 'sub_heads',
     }

    heads = fields.Many2one('accounting_heads.accounting_heads', string='Heads')
    sub_heads = fields.Many2one('accounting_heads.sub_heads',domain = "[('heads', '=', heads)]", string= 'Sub Heads')
    elementary_heads = fields.Char(string = 'Elementary Head')

这是我的观点:

 <record model="ir.ui.view" id="accounting_heads.elementary_form">
      <field name="name">Form</field>
      <field name="model">accounting_heads.elementary_head</field>
      <field name="arch" type="xml">
        <form >
          <field name="heads" string="Head"/>
          <field name="sub_heads" string="Sub Head"/>
          <field name="elementary_heads" string="Elementary Head"/>
        </form>
      </field>
    </record>

我想问题是您没有在 sub_heads 模型中创建任何字段 name,因此 Many2one 字段 sub_heads 的显示名称取值 heads 字段默认。

尝试在您的 accounting_heads.sub_heads model/class 中添加以下代码:

@api.multi
def name_get(self):
    result = []
    for sub_head in self:
        result.append((sub_head.id, sub_head.display_name))
    return result

@api.multi
@api.depends('sub_heads')
def _compute_display_name(self):
    for sub_head in self:
        sub_head.display_name = sub_head.sub_heads

其他更快的选择是将以下行添加到相同的 model/class:

_rec_name = 'sub_heads'