首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么与X模型相关的Many2one字段显示Y模型的选项?

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

Stack Overflow用户
提问于 2018-12-21 05:32:17
回答 1查看 116关注 0票数 2

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

代码语言:javascript
复制
sub_heads = fields.Many2one('accounting_heads.sub_heads',domain = "[('heads', '=', heads)]", string= 'Sub Heads')

尽管与Many2one模型有一个accounting_heads.sub_heads关系,但它显示的是accounting_heads.accounting_heads模型的记录。

为什么?

以下是我的模特:

代码语言:javascript
复制
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')

以下是我的观点:

代码语言:javascript
复制
 <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-21 10:32:49

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

尝试在accounting_heads.sub_heads模型/类中添加以下代码:

代码语言:javascript
复制
@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

其他更快的选项是在相同的模型/类中添加以下一行:

代码语言:javascript
复制
_rec_name = 'sub_heads'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53879650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档