Odoo Help

Welcome!

This community 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.

0

How to test if external id exists in python?

By
Pascal Tremblay
on 3/11/15, 11:37 AM 948 views

Hello there,

My custom module controller make a string like « point_of_sale.index_2 ».

In Odoo, python, how to test if this external ID exists in my DB before to use it later in the code?

If I use this external ID in my python code and it doesn't exist, I get an error.

Thanks for your idea.

1
Emanuel Cino
On 3/11/15, 12:42 PM

You could surround your code using that external_id with try/catch and do the appropriate thing in the catch clause if that id does not exist. If you need to do that check prior to the actual use of it you could use the method get_object_reference from ir_model_data and surround the code with try/except, like this:

try:

self.pool.get('ir.model.data').get_object_reference(cr, uid, 'module_name', 'external_id')

except ValueError:

# do whatever

Very interesting. I test it soon and I come back.

Pascal Tremblay
on 3/11/15, 12:43 PM

Sorry,I meant except, not catch...

Emanuel Cino
on 3/11/15, 1:35 PM

Thanks for your answer. Exactly what I needed.

Pascal Tremblay
on 3/11/15, 2:42 PM

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 community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/11/15, 11:37 AM
Seen: 948 times
Last updated: 3/16/15, 8:10 AM