3
votes

Thanks to the help of smart people on this site, I now have a nice One2many field in my module that allows me to add multiple order lines just like in the sales module. It works quite well, but now for convenience, I would like to be able to see a certain field within that One2many field in my tree and calendar views. However, when I try to display that field with the method described below, all I get is the number of records. In particular, I want it to display all of the products that were added to the order lines. Here is the relevant code:

models.py

# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.addons import decimal_precision as dp

class mymodule_base(models.Model):
    _name = 'mymodule.mymodule'
    _description = 'My Module'

    operations = fields.One2many('mymodule.line', 'order_id', 'Operation Lines')

class mymodule_line(models.Model):
    _name = 'mymodule.line'
    _description = 'Order Line'

    order_id = fields.Many2one('mymodule.mymodule')
    product_id = fields.Many2one('product.template', string='Repair / Part', required=False)

views.xml

<record id="mymodule.calendar" model="ir.ui.view">
    <field name="name">MyModule Calendar</field>
    <field name="model">mymodule.mymodule</field>
    <field name="arch" type="xml">
        <calendar string="Repairs" date_start="date_received" date_stop="date_due" color="state">
            <field name="operations">
                <field name="product_id"/>
            </field>
        </calendar>
    </field>
</record>

<record model="ir.ui.view" id="mymodule.list">
    <field name="name">MyModule list</field>
    <field name="model">mymodule.mymodule</field>
    <field name="arch" type="xml">
        <tree>
            <field name="operations">
                <field name="product_id"/>
            </field>
        </tree>
    </field>
</record>

Here is an image of what my list view looks like with the above code: List View

I would greatly appreciate any help. Please let me know if I need to further clarify what I'm trying to do.

EDIT I have tried Juan's suggestion, but I still only see numbers, not the product names, in my view. Here is what I did. I am not sure if I followed the instructions correctly.

Under mymodule.line

    product_name = fields.Char(related='product_id.name', store=True)

Under list view

            <field name="operations" widget="one2many_list" mode="tree">
                <field name="product_id" invisible="True"/>
                <field name="product_name"/>
            </field>

It doesn't seem to matter whether or not I include these two lines under the operations definition.

                <field name="product_id" invisible="True"/>
                <field name="product_name"/>

I still get the same result.

EDIT 2 I have tried adding a related field under mymodule.mymodule as such:

product_name = fields.Char(related='operations.product_id.name')

Then, I define this field in my list view like so:

<field name="product_name"/>

And this gives me almost what I want, except it only displays the name of one product, not all of them.

1

1 Answers

2
votes

Did you try to add the proper widget="one2many_list" for your operations field? like the lines of an invoice LINK. I hope this can help you.

EDIT:

Working with One2many related field the documentation says:

mode

for One2many, display mode (view type) to use for the field's linked records. One of tree, form, kanban or graph. The default is tree (a list display)

First, add mode attribute beside widget definition, and set the mode that you are looking for. Second, to get the name of the products, you should create a related field in your 'mymodule.line', something like this:

product_name = fields.Char(related='product_id.name', store=True)

And modify your view, to set product_id invisible="true", and then add <field name="product_name"/>

Warning: Do not avoid the product_id definition in your view, because the related field could not work properly

Information about related field HERE.

EDIT 2:

For your list view try this:

<record model="ir.ui.view" id="mymodule.list">
<field name="name">MyModule list</field>
<field name="model">mymodule.mymodule</field>
<field name="arch" type="xml">
        <field name="operations" widget="one2many_list">
            <tree>
                <field name="product_id" invisible="True"/>
                <field name="product_name" />
            </tree>
        </field>
</field>

And for your calendar view shoud follow the same structure.