Odoo Help


How to filter by external id in domain?

Justin Lessard
on 9/10/15, 10:30 AM 1,587 views

I'm trying to constraint the domain of a many 2 one field using an external id (The XML id).

I want to be able to choose product which are variant of a specific product template.

#This piece of code doesn't work
the_field = fields.Many2one('product.product', domain = [('product_tmpl_id','=', "ref('the_package.the_external_id')")])

The code above doesn't work for some reason. But I've been able to look in the database to resolve the external id and it's 56. With that knowledge, I've been able to hardcode the following line and make everything work.

#This piece of code works, but it's hardcoded

the_field = fields.Many2one('product.product', domain = [('product_tmpl_id','=', 56)])

How can I do to solve this using the External id and not the hardcoded id in database?

EDIT: I cannot access all the self variables (self.env, self.pool, ...) since I'm in a static context. 

Justin Lessard
On 9/10/15, 12:28 PM

I've found the solution to my problem with the help of Kinner Vachhani.

I simply needed to create a function which will return the domain filter like this:

def _get_domain(self):

    ids = self.env.ref('the_package.the_external_id').ids
return [('product_tmpl_id','=', ids)]

the_field = fields.Many2one('product.product', required=False,   domain = _get_picking_product_domain)
Kinner Vachhani
On 9/10/15, 11:18 AM

You can get res_id (resource id) by searching ir_model_data. 

imd_obj = self.pool['ir.model.data']
ext_ids = imd_obj._get_id('module_name', 'external_identifier')

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

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: 9/10/15, 10:30 AM
Seen: 1587 times
Last updated: 11/12/15, 11:05 PM