Skip to Content
Menú
This question has been flagged
6 Respostes
33665 Vistes

Hi Guys,

Is it possible to be able to create a search filter in Odoo based on a calculated/computed field?  It appears it is not possible?

Thanks,
Damien

Avatar
Descartar
Best Answer

We can search non stored field like this :

field_name = fields.Boolean(compute="compute_meth", search='_value_search')

@depends('dependent_field')

def compute_meth(self):

# Your compted code

then

@api.multi

def _value_search(self, operator, value):

    recs = self.search([]).filtered(lambda x : x.field_name is True )

    if recs:

               return [('id', 'in', [x.id for x in recs])]

Now you can get it in list mode as well.

Avatar
Descartar

What it does is, whenever you searched in the filter the search method calls the computed method and it returns the value.

Best Answer

Let me precise the answer form Annadurai with an example:

field_x = fields.Integer('Calculated Value', compute="_compute_field" search="_search_field")

@api.multi
def _search_field(self, operator, value):
field_id = self.search([]).filtered(lambda x : x.field_x == value )
return [('id', operator, [x.id for x in field_id] if field_id else False )]
Avatar
Descartar
Best Answer

I had same problem in having a computed field in filter

The only way is to define a non computed field from the computed one and put it in view. then it will appear in filter

Example:

payment_id = fields.Integer(compute='compute_lastpayment')

payment_state = fields.Char(string='Last Payment State')

 

    @api.multi

    def compute_lastpayment(self):

                for order in self:

                                if order.payment_ids:

                                                payment = order.payment_ids[0]

                                                order.write({'payment_state': payment.state})

 

 

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

        <field name="name">purchase.order.tree.inherit</field>

        <field name="model">purchase.order</field>

        <field name="inherit_id" ref="purchase.purchase_order_tree"/>

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

                 <field name="payment_id" invisible="1"/>

                <field name="payment_state"/>

            </field>

        </field>

    </record>

 

Avatar
Descartar

thank you very much for this solution, I tested it on version 13 and it worked!

Best Answer

Only if you store the field.

See the example of the field END_DATE in the DEFAULT VALUES section of  http://www.odoo.com/documentation/10.0/howtos/backend.html#computed-fields-and-default-values

Avatar
Descartar
Best Answer

A compute field, by default, is not stored on database. You just need to add stored=True yo the field in Python file. That allows you to filter/group by computed field.

Avatar
Descartar

He is asking about filters, Normally these fields are stored if store = True, but the field is updated it still shows the old values. For that, my solution will work for his case.

Autor Best Answer

Tried add this attribute, but still did not show up in the filter dropdown:

approver_ids = fields.Many2many('res.users', compute="_get_approvers", string="Approvers", stored=True)

also tried

approver_ids = fields.Many2many('res.users', compute="_get_approvers", string="Approvers", stored="True")


However in the filter dropdown, there is no "Approvers" option


Avatar
Descartar

I think it should be `stored=True` (remove the quotes)