I need to access the last manytomany filed id in odoo
- User
I need to access the last manytomany filed id in odoo
Assistant
You can access the last many2many record ID in Odoo using the following steps:
-
Get the model and field name of the many2many relation. For example, if Product has a many2many relation with Category, the model would be product.category and the field name would be categ_ids.
-
Search for the records in that model ordered by ID descending:
last_m2m = request.env['product.category'].search([], order='id desc', limit=1)- The ID of the first record in the search result will be the last many2many ID. You can access it as:
last_m2m_id = last_m2m.idSo the full code would be:
last_m2m = request.env['product.category'].search([], order='id desc', limit=1) last_m2m_id = last_m2m.idThis will give you the ID of the last product category, which is the last many2many ID for the product.categ_ids field.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.