Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Search with functional fields

By
Bruno Cascio
on 4/21/14, 1:26 AM 5,425 views

How to search with functional fields?

my code

'calculate_next_visit' : fields.function( 
     __get_next_visit,
    method=True,
    type='char',
    store=True,
    string="Calculo de próxima visita"),

 def __get_next_visit(self, cr, uid, obj, name, args, context):
      print 'something'
      res = {}
      for v in self.browse(cr, uid, obj):
         diagnosis = map(lambda x:x.upper(),v.diagnosis)
         if 'PAP' in diagnosis or 'P.A.P' in diagnosis:
           # more code....
            res[v.id] = True
      return res

and my view:

<record id="search_visit_filter" model="ir.ui.view">
      <field name="name">Visitas</field>
      <field name="model">visit</field>
      <field name="arch" type="xml">
        <search string="Search xxx">
            <filter domain="[('calculate_next_visit','=',True)]" string="Visitas Próximas a la fecha"/>
        </search>
      </field>
    </record>

When select filter, never print "something"

Somebody Help me?

THE SOLUTION

'calculate_next_visit' : fields.function(
         _next_visit,
         fnct_search=_search_next_visit,
         method=True, 
         type='char', 
         multi=True,
         string="Calculo de próxima visita", 
      ),

set fnct_search parameter, and implement that.

def _search_next_visit(self, cr, uid, obj, name, args, context=None):
      res = []
      ids = obj.search(cr,uid,[]) # ids of visits
      for v in self.browse(cr, uid, ids): # foreach visit
         diags = [d.name.upper() for d in v.diagnosis]
         if "PAP" in diags:
            res.append(v.id)
      return [('id','in',res)]

important return type of fnct_search

and my search view...

<group expand="0" string="Agrupar por...">
            <filter string="Próximas Visitas" domain="[('calculate_next_visit','=',True)]" context="{'group_by':'next_visit'}" />
</group>

check documentation for fnct_search functions

@ Bruno , i have the same problem as you . You already found the answer right ? did you change __get_next_visit method as well ?

Ajeng Shilvie
on 12/30/14, 1:44 AM
4
Roc cheng
On 4/21/14, 2:26 AM

Hi, It looks like you just want testing whether the __get_next_visit on working.. but on serach view,When select filter,this way won't trigger the function. because the object never changed.

Thanks! There is the possibility of implementing a filter to only such rows that meet a condition such that a date approximates the current date?

Bruno Cascio
on 4/21/14, 5:01 PM
3

Dhinesh - Technical Consultant, Sodexis Inc

--Dhinesh - Technical Consultant, Sodexis Inc--
3577
| 5 4 9
Pondicherry, India
--Dhinesh - Technical Consultant, Sodexis Inc--

Passionate coder in Python/OpenERP. Knows Django/Flask MVC frameworks. Did code in Java, PHP.

Contact me at: dvdhinesh.mail@gmail.com

Dhinesh - Technical Consultant, Sodexis Inc
On 4/21/14, 2:47 AM

Hi, Functional fields get triggered at the time of saving the record. If u want to trigger at the time of loading try to give in _defaults.

I tried a default field, and I was not successful. Could you explain please?

Bruno Cascio
on 4/21/14, 5:02 PM
0
xingtongjie
On 9/3/14, 9:19 PM

let  function field `s store parameter equal True

0

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 9/4/14, 2:24 AM

Hi...

Firstly, your data to that field is not correct, I mean you have defined functional field for "Char" whereas your function is returning True which is of type "Boolean"... so loook into your code again...

Then coming to search criteria, A functional field is of two type.. One having a "store" property and one without it..

1. when store is set True: then the functional field and its value will be saved in the database, which will act just like other physical fields, so you can specify it in search without any issue..

2. when store is not set: then the functional field will be like evalution field, meaning value for it will be evaluated/computed only when it is triggered... henceforth one cannot use it in search view, as the field and its data are not stored in db... So in order to search, you should write "fnct_search" property to your function field and define it accordingly...

Check this link for more details: Function Fields

You can find many examples in addons.

Hope this will help you.

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/21/14, 1:26 AM
Seen: 5425 times
Last updated: 3/16/15, 8:10 AM