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

时间:2018-12-21 05:32:17

标签: python xml odoo

我的自定义模型中有一个名为accounting_heads.elementary_head的字段:

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

尽管与accounting_heads.sub_heads Many2one 关系 模型,它显示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>

1 个答案:

答案 0 :(得分:2)

我想问题是您没有在name模型中创建任何字段sub_heads,因此Many2one字段sub_heads的显示名称采用了{{1 }}字段为默认值。

尝试在您的heads模型/类中添加以下代码:

accounting_heads.sub_heads

另一个更快的选择是将以下行添加到相同的模型/类中:

@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