Skip to Content
Menu
This question has been flagged
3 Replies
2706 Views

I need to be able to filter and group by pricelist on contacts, The technical field name is '

property_product_pricelist = fields.Many2one(
comodel_name='product.pricelist',
string="Pricelist",
compute='_compute_product_pricelist',
inverse="_inverse_product_pricelist",
company_dependent=False,
domain=lambda self: [('company_id', 'in', (self.env.company.id, False))],
help="This pricelist will be used, instead of the default one, for sales to the current partner")

'

I am afraid that if I add store=True, it would break something in Odoo. I'm not sure

Avatar
Discard
Best Answer

you can do it with partner search module provided by OCA

https://apps.odoo.com/apps/modules/17.0/partner_pricelist_search

Avatar
Discard
Best Answer

Hiii,

To make a field searchable (filterable or groupable in the UI), it must be stored in the database . So yes, to enable filtering/grouping by pricelist, you must add store=True :

property_product_pricelist = fields.Many2one(

    comodel_name='product.pricelist',

    string="Pricelist",

    compute='_compute_product_pricelist',

    inverse="_inverse_product_pricelist",

    store=True,

    company_dependent=False,

    domain=lambda self: [('company_id', 'in', ( self.env.company.id , False))],

    help="This pricelist will be used, instead of the default one, for sales to the current partner"

)


Will store=True Break Anything?
In most cases — no , as long as:

  1. The compute method is always valid and gives a consistent result .
  2. You have an inverse method (which you do: _inverse_product_pricelist ).
  3. You're not depending on live computation for transient values (not true here).

This is a perfect case to use store=True :

  • It improves performance (no live compute needed on every record access)
  • It enables proper filtering & grouping
  • Odoo's default behavior is to store fields like this unless explicitly meant to be dynamic

i hope it is use full

Avatar
Discard
Best Answer

Hi,

If you want to filter and group by property_product_pricelist on res.partner without storing the field, you need to define a _search method in Python. This tells Odoo how to search on a non-stored computed field, so it can be used in search views for filters and group_by.


Please refer to the code below:


from odoo import models, fields, api


class ResPartner(models.Model):

    _inherit = 'res.partner'


    property_product_pricelist = fields.Many2one(

        comodel_name='product.pricelist',

        string="Pricelist",

        compute='_compute_product_pricelist',

        inverse="_inverse_product_pricelist",

        search='_search_property_product_pricelist',  # Enable filter/group_by

        company_dependent=False,

        domain=lambda self: [('company_id', 'in', (self.env.company.id, False))],

    )


    @api.model

    def _search_property_product_pricelist(self, operator, value):

        # This assumes the pricelist is stored or linked through some relation

        return [('property_product_pricelist', operator, value)]


XML- Filter:


<record id="view_res_partner_filter" model="ir.ui.view">

       <field name="name">res.partner.search.inherit.pricelist</field>

       <field name="model">res.partner</field>

       <field name="inherit_id" ref="base.view_res_partner_filter"/>

       <field name="arch" type="xml">

           <xpath expr="//filter[@name='user_id']" position="after">

               <filter string="Pricelist" name="group_pricelist" domain="[]" context="{'group_by': 'property_product_pricelist'}"/>

           </xpath>

       </field>

   </record>


Hope it helps.

Avatar
Discard
Related Posts Replies Views Activity
2
Dec 24
4171
0
Nov 23
1376
0
Oct 23
2193
0
Jul 25
6217
1
Jul 25
2250