Odoo Help


domain filter on many2one field, depending on fields of the actual model

Martin Varela
on 10/22/14, 1:35 PM 2,076 views

I´m trying to apply a domain on a many2one field based on the fields of both the actual model and the many2one model.

class a_class(osv.osv):
    _name = "class_a"
    _columns = {
        'the_code': fields.integer('Code'),
        'category_id': fields.many2one('categories','Category'),
        'invoice_id': fields.many2one('account.invoice', 'Invoice'),

The model 'categories' contains a field integer field 'code'

I want to filter in field category_id only categories with the same value in code as "the_code" in class_a, when invoice_id is not null
I´ve tried with:
<field name="category_id" domain="['|',('code','=',the_code),('invoice_id','=',False)]"/>
But it gives me the error:
ValueError: Invalid field 'invoice_id' in leaf "<osv.ExtendedLeaf: ('invoice_id', '=', False) on categories (ctx: )>"
because is looking for the field invoice_id in the many2one model and not in the actual model.

Is it possible in a many2one field to define a domain based on fields of the current model? (NOT in the many2one model)

The only solution I´ve found till now is to add 2 times the field category_id and make it invisible depending on the field invoice_id. When invoice_id is not null I apply de domain:
<field name="category_id" domain="[('code','=',the_code)]" attrs="{'invisible': [('invoice_id','=',False)]}"/>
<field name="category_id" attrs="{'invisible': [('invoice_id','!=',False)]}"/>

Any solutions?


Ben Bernard

--Ben Bernard--

| 4 3 6
Jakarta, Indonesia
--Ben Bernard--
Ben Bernard
On 10/22/14, 11:44 PM

It seems like the 'categories' model doesn't has invoice_id field. If that so, then it's not make sense to filter category_id domain based on invoice_id.

I want to filter the domain on category_id when invoice_id is not null. invoice_id has nothing to do with categories, it´s just another field of the model "class_a"

Martin Varela
on 10/23/14, 7:52 AM

you're defining domain for a field, category_id, so the domain filter should available in the same model as category_id which is categories. back to your question, "Is it possible in a many2one field to define a domain based on fields of the current model?" no. it easier to add invoice_id in categories model.

Ben Bernard
on 10/23/14, 10:59 AM

It´s not easier, it doesn´t make sense adding a invoice_id in the categories model beacuse the invoice has nothing to do with categories. Imagine this scenario where I have 2 different objects of class_a with the same category but different invoice_id: class_a1: category_id:1, invoice_id:10 class_a2: category_id:1, invoice_id: 20

Martin Varela
on 10/24/14, 11:06 AM

I agree if you don't want to categorized thing based on invoice_id. My point is your requirement imply that you need some other field in categories model related with the invoice. What about a boolean to indicated that the category object is invoiced or not. Then you can filter category_id with this boolean, easily.

Ben Bernard
on 10/24/14, 11:53 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 10/22/14, 1:35 PM
Seen: 2076 times
Last updated: 3/16/15, 8:10 AM