Ir al contenido
Menú
Se marcó esta pregunta

Hi,

I created a new Many2one field called grade on the stock.lot model. The grade model contains several fields, one of which is a color field:

color = fields.Integer(string="Color")

My goal is to display serial numbers with different colors in the picking view.

I tried to modify the many2many_tags widget in stock.view_picking_form using XPath to add a color_field option:

<xpath expr="//field[@name='lot_ids']" position="attributes"> <attribute name="options"> {'create': [('parent.use_create_lots', '=', True)], 'color_field': 'product_grade_id.color'} </attribute> </xpath>

However, it seems many2many_tags cannot access a child field (product_grade_id.color) directly.

As a workaround I added a related field on stock.lot that copies the grade color:

grade_color = fields.Integer( related="product_grade_id.color", string="Grade color", readonly=True, )

This works, but feels like a workaround. Is there a way to make many2many_tags use the child field directly, or a better pattern to achieve colored serial numbers in the picking view?

Thanks in advance for any suggestions.

Avatar
Descartar

Hello,
The many2many_tags widget can only use fields that exist directly on the same model. It seems adding a related field is the correct solution.

Mejor respuesta

Hi,


The many2many_tags widget in Odoo expects the color_field option to reference a direct field of the target model (in this case, stock.lot).

It cannot traverse relations like product_grade_id.color because the widget operates client-side in the web view and doesn’t perform nested record fetching, it just uses what’s directly in the record’s dataset.


Try the following,


Python


class StockLot(models.Model):

    _inherit = "stock.lot"


    product_grade_id = fields.Many2one('product.grade', string="Grade")

    grade_color = fields.Integer(

        related="product_grade_id.color",

        string="Grade color",

        store=True,  

        readonly=True,

    )


XML


<xpath expr="//field[@name='lot_ids']" position="attributes">

    <attribute name="options">

        {'create': [('parent.use_create_lots', '=', True)], 'color_field': 'grade_color'}

    </attribute>

</xpath>


Hope it helps


Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
3
may 24
4337
2
mar 15
5211
0
nov 18
4946
3
jun 25
2410
3
nov 23
3026