在model2的树形视图中显示model1中的字段

时间:2017-10-12 04:02:23

标签: python python-2.7 openerp odoo-10

我想显示来自payroll.adjustment.lines的字段,即employee_id,并从payroll.adjustment模型的树形视图中显示该字段。可能吗?他们有关系。

来自payroll.adjustment模型

adjustment_lines = 
fields.One2many('payroll.adjustment.lines','adj_id',string="Adjustment 
lines")
来自payroll.adjustment.lines模型

adj_id = fields.Many2one('payroll.adjustment',string="Payroll 
Adjustment",ondelete='cascade')

和我的xml

<record id="payroll_adjustment_tree_view" model="ir.ui.view">
        <field name="name">payroll_adjustment.tree</field>
        <field name="model">payroll.adjustment</field>
        <field name="arch" type="xml">
            <tree string="Payroll Adjustment" colors="red:state == 
            'void';green:state == 'draft';blue:state=='confirm'">
                <field name="doc_num"/>
                <field name="company_id"/>
                <field name="adjustment_lines"/>
                <field name="date_from"/>
                <field name="date_to"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

<field name="adjustment_lines"/>

仅显示(2条记录)而不是员工姓名。请帮帮我们。感谢

我尝试了下面的答案,这就是结果。员工姓名显示错误

Result

这是我的树视图,我从行调用字段并从payroll.adjustment模型显示到树视图。

tree view code

这是我的树视图的输出,它只显示(记录)

tree view output

1 个答案:

答案 0 :(得分:1)

当您覆盖模型name_get()的{​​{1}}方法时,它可以正常工作。以下代码示例有希望自我解释,并为您的案例提供一般示例:

payroll.adjustment.line

from odoo import models, fields, api class MyModel(models.Model): _name = "my.model" another_model_ids = fields.One2Many( comodel_name="another.model", inverse="my_model_id", string="Another Model Entries") class AnotherModel(models.Model): _name = "another.model" my_model_id = fields.Many2One( comodel_name="my.model", string="My Model") number = fields.Integer(string="A Number") yet_another_model_id = fields.Many2One( comodel_name="yet.another.model", string="Yet Another Model") @api.multi def name_get(self): # with context flags you can implement multiple # possibilities of name generation # best example: res.partner res = [] for another_model in self: res.append((another_model.id, "{} {}".format( another_model.number, another_model.yet_another_model_id.name))) return res class YetAnotherModel(models.Model): _name = "yet.another.model" name = fields.Char(string="Name") 将是您的my.modelpayroll.adjustment行,another.model将是yet.another.model hr.employee背后的模型。