Skip to main content
 首页 » 编程设计

python之在模型 2 的 TreeView 中显示模型 1 的字段

2025年01月19日18lvdongjie

我想显示 payroll.adjustment.lines 中的字段,即 employee_id,并从 payroll.adjustment 模型的 TreeView 中显示它。是否可以?他们有关系。

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

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

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

这是我的 TreeView 的输出,它只显示(记录)

请您参考如下方法:

当您覆盖模型 payroll.adjustment.line 的 name_get() 方法时,它可能会起作用。以下代码示例希望是 self 解释的,并且是您案例的一般示例:

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.adjustment,another.model 行和 yet.another.model 将是 hr.employee employee_id 背后的模型。