콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
6 답글
33775 화면

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

아바타
취소
베스트 답변

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.

아바타
취소

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

베스트 답변

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 )]
아바타
취소
베스트 답변

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>

 

아바타
취소

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

베스트 답변

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

아바타
취소
베스트 답변

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.

아바타
취소

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.

작성자 베스트 답변

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


아바타
취소

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