4

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.

Colinito
  • 115
  • 2
  • 10

1 Answers1

2

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.

Community
  • 1
  • 1
Juan Salcedo
  • 1,598
  • 1
  • 16
  • 28
  • I tried your suggestion, but it only changed the field from displaying the text "(5 records)" to "384,385,386,387,388". I want it to display the name of the product in each record (product_id). It seems to be showing values for the operations field, but not the product_id field within it. I should also point out that I'm not talking about the tree view within my form view, but the list view that the module opens up to initially. I'd also like it to work in the calendar view as well. – Colinito Oct 06 '17 at 15:13
  • Thanks for the suggestion, but it didn't seem to work for me. I updated my question with the new code. Please let me know if I did it correctly. I may not be following you 100%. Again, it only shows the numbers, not the product names. – Colinito Oct 06 '17 at 16:37
  • Unfortunately, it still doesn't work. The same thing happens: it just displays numbers. I think that the field product_name defined underneath the operations field is not being shown. Only the values of the operations field are being shown. I have updated my question again just showing an alternate solution I tried. This is almost what I want, but it doesn't display all of the product names, only one of them. – Colinito Oct 06 '17 at 18:06
  • 1
    These suggestions, although insightful, don't work for the ultimate purpose. If any of you has an actual solution, please keep this post updated, I'm having the same issue – damores Mar 04 '18 at 11:11
  • me too, I have searched many source for how to make graph view from multiple models related as many2one or many2many – Sal Dec 31 '22 at 19:21