gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:06 25 4
gpt4 key购买 nike

我的自定义模型中有以下字段,名为 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 模型/类中添加以下代码:

@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

其他更快的选择是将以下行添加到同一模型/类:

_rec_name = 'sub_heads'

关于python - 为什么与X模型相关的Many2one字段显示Y模型的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com