Hi Uzair
Try to use a default attribute to populate the field when creating a new record.
from odoo import models, fields
class Partner(models.Model):
_inherit = 'res.partner'
_description = 'Contact'
def _default_category(self):
# Return the default category based on context or other logic
return self.env['res.partner.category'].browse(self._context.get('category_id'))
category_id = fields.Many2many(
'res.partner.category',
column1='partner_id',
column2='category_id',
string='Tags',
default=_default_category, # Use default instead of compute
store=True
)
Explanation
- default Instead of compute: The default attribute allows Odoo to populate the Many2many field with an initial value at record creation. By using default, values are stored directly in the database, making the field accessible for grouping.
- Store Values in Database: With store=True and the default value set, category_id values are saved to the database, allowing you to group records by this field.
Let me know if that helped
warm regards
Daniel