M.E.
M.E.

Reputation: 5495

Odoo 10 - Get id of a specific record from product_uom

I have the following custom UoM defined in the system.

I have an extended model for product and I would like to look programmatically for the id of this specific record, so I can use it in an onchange method.

As the type is created manually, I do not want to hardcode the id as other UoMs might have been defined previously. How do you get from this model (product.uom) the specific id which corresponds to "name" = "MILES" ?

 id | create_uid |  name  | rounding | write_uid | uom_type |         write_date         | factor | active |        create_date         | category_id
----+------------+--------+----------+-----------+----------+----------------------------+--------+--------+----------------------------+-------------
 20 |          1 | MILES  |    0.001 |         1 | bigger   | 2017-07-12 03:42:25.363007 |  0.001 | t      | 2017-07-12 03:33:27.251635 |           1

Upvotes: 2

Views: 1684

Answers (2)

CZoellner
CZoellner

Reputation: 14778

A second approach besides Odedra's is to use external IDs. But you have to either give the unit one by yourself in the client (under Settings) or you provide the unit within your custom module as xml data record, which already require an ID.

Let's say the external ID is my_module.my_unit_of_measure, in Odoo python code you will be able to get it by:

self.env.ref('my_module.my_unit_of_measure')

In xml or csv files there are some possibilities, too:

<record id="my_product" model="product.product">
    <!-- required fields -->
    <field name="uom_id" ref="my_module.my_unit_of_measure" />
</record>

<!-- using in filters should work too -->
<filter name="filter_my_unit" string="Products with my unit"
    domain="[('uom_id', '=', ref('my_module.my_unit_of_measure')" />

Upvotes: 1

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

You can try with following ORM search() method:

self.env['product.uom'].search([('name', '=', 'MILES')], limit=1).id

Upvotes: 3

Related Questions