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

2024-04-29 13:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想显示工资单.调整.行它是雇员的id,并从工资调整模型。有可能吗?他们有关系。你知道吗

从工资调整型号

adjustment_lines = 
fields.One2many('payroll.adjustment.lines','adj_id',string="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

这是我的树状视图,在这里我调用了行中的字段,并从我的工资调整模型。你知道吗

tree view code

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

tree view output


Tags: name模型idtreefieldfieldsstringpayroll
1条回答
网友
1楼 · 发布于 2024-04-29 13:22:47

当您重写模型payroll.adjustment.linename_get()方法时,它就可以工作了。下面的代码示例是自我解释,也是您案例的一般示例:

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.model就是你的payroll.adjustmentanother.model就是那条线,yet.another.model就是hr.employee背后的模型。你知道吗

相关问题 更多 >