RobbeM
RobbeM

Reputation: 757

Odoo: One2many field not working

I've encountered strange behaviour of a one2many field in Odoo.

This is my code:

models.py:

class mrp_bom_inherit(models.Model): 
    _inherit = 'mrp.bom'
    producten_tussenmodel = fields.One2many(comodel_name='tussenmodel_wc_producten', inverse_name='bom_id', string="producten", copy=True)

class tussenmodel_wc_producten(models.Model):
    bom_id = fields.Integer()
    routing_id = fields.Integer()
    producten = fields.Char(string="Productnr.")

views.xml:

<field name="producten_tussenmodel" widget="one2many_list" nolabel="1">
    <tree string="Een Naar Veel" editable="bottom">
        <field name="producten" domain="[('routing_id', '=', 32)]"/>
    </tree>
</field> 

This gives as output:

enter image description here

But it should only show "bbb" since "bbb" is the only record in the database with routing_id = 32 in table tussenmodel_wc_producten and bom_id in table tussenmodel_wc_producten equals an id in mrp.bom.

I've checked this via an SQL query:

select producten
from mrp_bom as m JOIN tussenmodel_wc_producten as t ON(m.id = t.bom_id) 
where t.routing_id = 32

Which has as only output "bbb".

What am I doing wrong here?

Edit: Some more screenshots for Ludwik Trammer: enter image description here enter image description here

Upvotes: 3

Views: 3298

Answers (3)

Adrian Merrall
Adrian Merrall

Reputation: 2499

A possible problem, on your one2many field, the inverse argument is suppose to point to a many2one on the other table, i.e. the inverse of the relationship, whereas in your code above, bom_id in tussenmodel_wc_producten is an Integer.

You should redefine as

fields.Many2one("mrp.bom", "BOM Id")

One final thing; tussenmodel_wc_producten doesn't define an _name for the model. This is legal as the base model will default to class.name if _name isn't defined, but it is convention to either define _name or _inherit. Given how important _name is in Odoo model processing, I wouldn't like to rely on everything working as expected if it isn't defined.

Upvotes: 0

First of all you need to update the relation in model, for One2many relation you must take Many2one field as inverse field name, So that odoo engine can interpret it properly.

class tussenmodel_wc_producten(models.Model):
    bom_id = fields.Many2one('mrp.bom','Bom')

And also update your field domain in that way,

<field name="producten_tussenmodel" widget="one2many_list" nolabel="1" domain="[('routing_id', '=', 32)]">
    <tree string="Een Naar Veel" editable="bottom">
        <field name="producten" />
    </tree>
</field> 

Upvotes: 0

Ludwik Trammer
Ludwik Trammer

Reputation: 25072

domain attribute on a field only controls which objects are present on a list of available objects (i.e. objects that can potentially be chosen for the relation) when the user edits a form. It doesn't do anything more than that and specifically it doesn't control which objects are actually in the relation.

If the objects is already in a relation with "aaa", "bbb", "ccc" and "ddd", setting a domain won't do anything to change that.

Upvotes: 1

Related Questions