Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
3 Antwoorden
2066 Weergaven
.py
class Partner(models.Model):
_description = 'Contact'
_inherit = 'res.partner'
def _default_category(self):
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', store=True, compute =_default_category)

Actually i am trying to apply the group by on Many2many field but it does not store any value i dont know why. 


Avatar
Annuleer
Beste antwoord

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
  1. 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.
  2. 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

Avatar
Annuleer
Beste antwoord

Hi

we can create an alternate Char field to store these values in M2M and show it in the group by 

Check these references it will help you to create a group for many2many field 

How to Use Group for Many2Many Fields in Odoo


Regards



Avatar
Annuleer
Auteur Beste antwoord

thanks @Milay shajan

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
0
apr. 24
1608
0
feb. 23
2685
1
aug. 21
11228
1
feb. 25
20822
1
apr. 16
6371