law rence
law rence

Reputation: 179

Displaying field from model1 in the tree view of model2

I want to display the field from payroll.adjustment.lines which is employee_id and display it from the tree view of payroll.adjustment model. Is it possible? They have relation.

from payroll.adjustment model

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

from payroll.adjustment.lines model

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

and in my 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>

the

<field name="adjustment_lines"/>

only display (2 records) not the employee name. Please help me guys. Thanks

I tried the answered below and this is the result. The employee name displaying false

Result

and this is my tree view where i called the field from lines and display it to tree view from my payroll.adjustment model.

tree view code

and this is the output of my tree view and it only shows (records)

tree view output

Upvotes: 2

Views: 126

Answers (1)

CZoellner
CZoellner

Reputation: 14768

It could work, when you override the name_get() method of model payroll.adjustment.line. Following code example is hopefully self explanory and a general example for your case:

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 would be your payroll.adjustment, another.model the line and yet.another.model would be hr.employee the model behind employee_id.

Upvotes: 1

Related Questions