【问题标题】:Displaying field from model1 in the tree view of model2在模型 2 的树视图中显示模型 1 的字段
【发布时间】:2017-10-12 04:02:22
【问题描述】:

我想显示 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 条记录)而不是员工姓名。请帮帮我。谢谢

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

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

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

【问题讨论】:

    标签: python python-2.7 openerp odoo-10


    【解决方案1】:

    当您覆盖模型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 employee_id 后面的模型。

    【讨论】:

    • 我在 XML 上调用的字段是 another_model_ids(adjustment_lines),其中给了我相同的结果,即(2 条记录)。我在 XML 上调用了哪个字段?抱歉,我是 Odoo 的新手,或者请给我一些有用的链接,以便我学习。感谢您的努力先生
    • 能否分享您问题中树视图的屏幕截图?
    • 我的意思是显示树的客户端屏幕截图,抱歉不够具体;-)
    • 哦,我完全错过了,你有一个 one2many 字段可以在树视图中显示。您可以尝试 2 件事:首先尝试将小部件 many2many_tags 用于字段 adjustmend_lines。如果这不起作用,因为小部件不能很好地与 one2many 字段一起工作,那么将计算字段添加到 my.model 这将创建一个逗号分隔的所有行字符串以在树视图中显示它们。跨度>
    • 谢谢@CZoellner。已经解决了。我所做的是创建写入函数并在其中创建一个带有列表的循环,并且每个循环都将附加到 var_list。我在FB的朋友也帮助了我。感谢您的努力和时间。
    猜你喜欢
    • 2018-05-09
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2013-12-10
    相关资源
    最近更新 更多