Odoo Help


How to update function field on button click

on 9/28/13, 6:44 AM 7,784 views
How can i update function field on button click. I want to display names of files in a directory on button click. i will get file names in a list . how can i show that in xml using function field ? Any one please help       
 My xml is
 <?xml version="1.0" encoding="utf-8" ?>
              <record id="voip_form_view" model="ir.ui.view">
                <field name="name">Voip Details</field>
                 <field name="model">filedata</field>
                  <field name="arch" type="xml">
                     <form string="voip" version="7.0">
                             <style>.openerp .oe_application 
                               .oe_breadcrumb_title  {display:none }</style>
                             <style>.openerp .oe_view_manager table.
                             oe_view_manager_header .oe_view_manager_buttons{display:none } 
                            <button name="get_filenames" type="object" string="List Files" />
         <record id="voipdata_tree_view" model="ir.ui.view">
              <field name="name">File Details</field>
               <field name="model">filedata</field>
                 <field name="priority" eval="1"/>
                 <field name="type">tree</field>
                 <field name="arch" type="xml">
                 <tree string="File Names" >
                     <field name="file_name"/>
                      <field name="time_created" string="Time Created"/>
                       <field name="size" string="Size"/>
                        <field name="file_path"/>
          <record model="ir.actions.act_window" id="action_voipdata_form">
              <field name="name">Voip Details</field>
               <field name="res_model">filedata</field>
                 <field name="view_type">tree</field>
                  <field name="view_mode">tree</field>
                   <field name="view_id" ref="voipdata_tree_view"/>
             <record model="ir.actions.act_window" id="action_voip_form">
                  <field name="name">Voip Details</field>
                    <field name="res_model">filedata</field>
                     <field name="view_type">form</field>
                      <field name="view_mode">form</field>
                       <field name="view_id" ref="voip_form_view"/>
            <menuitem name="Voip" parent="base.menu_reporting" id="voip_menu_mainform"/>
             <menuitem name="Voip" parent="voip_menu_mainform" id="voip_menu_subform" 
    My python code is
    class filedata(osv.osv):
                    _name = 'filedata'
                    _log_access = False
                    def compute(self, cr, uid, ids, field_names, arg, context):
                       result = {
                           2: {'file_name': 'Bob', 'file_path': 'ffff',
                           return result
                    _columns = {
                      'file_name' : fields.function
                                (compute, type='char', size=50,multi='person_data'),
                      'file_path' :fields.function
                              (compute, type='char', size=50,multi='person_data'),
                               (compute, type='char', size=50,multi='person_data'),
                       'size' : fields.function
                                (compute, type='char', size=50,multi='person_data')
                    def get_filenames(self, cr, uid, ids, context=None):
                        lst_dirfiles = os.listdir('/voice-recordings')
                        cr.execute("select id,file_name from filedata")
                        result = cr.fetchall()
                        lst_dbfiles = []
                        for res in result:
                            file_name = res[1]
                            if (file_name == None):
                                cr.execute("delete from filedata where id=%s",(res[0],))
                         if lst_dirfiles:
                            for file_name in lst_dirfiles:
                                if file_name not in lst_dbfiles:
                                  time_created = time.ctime(os.path.getctime
                                  size = str(os.path.getsize
                                               ("/voice-recordings/"+file_name))+' Bytes'
                                  file_path = "/voice-recordings/"+file_name
                                  file_base64 = ''
                                   file_object = open(file_path, "rb")
                                   file_base64 = base64.b64encode(file_object.read())
                                   file_path = "ftp://test.claudion.com:123/"+file_name
                                   cr.execute("inserit into filedata
                        return {
                            'type' : 'ir.actions.act_window',
                            'name' : 'File Details',
                            'view_mode' : 'tree',
                            'view_type' :  'form',
                            'res_model' : 'filedata',
                            'context' : context,

Andreas Brueckl

--Andreas Brueckl--

| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 9/28/13, 7:22 AM

What you want to accomplish is not that easy. Here are some hints for your implementation:

  • Use a parent object filedata with child object filedata.line
  • filedata has a function field lines of type one2many (relation filedata.line)
  • The compute function then returns the lines which you currently want to return in function get_filenames
  • Keep in mind that the function is called every time you open the view (so performance could be poor with this approach)

Here is another approach:

  • Use only one model filedata
  • Put the logic of get_filenames into a Server Action which you call by clicking onto a menu item
  • The server action then deletes all current records of filedata and creates a new entry for every record with self.pool.get('filedata').create ...
  • This approach only computes on button-click and is much faster when only displaying the data

Hope this helps.

@Andreas Brueck i checked sale/sale.py but i didnt understand anything. Can you please explain it to me

on 9/28/13, 7:32 AM

Function fields are automatically updated after a button click. If would help if you post your code in your question.

Andreas Brueckl
on 9/28/13, 7:40 AM

@Andreas Brueckl I added my code in question.my button calls get_filenames function which should return a view to display file names. In my code i inserted file names in to db in that function and displays it in view,. Now i dont want to save anything in db . that is my issue please help

on 9/28/13, 8:02 AM

@Andreas Brueck Any other way to achive this ? Please help !! I have been on this for long time :(. I dnt want to use function field i want to display file name in view and play that mp3 files from there

on 9/28/13, 8:19 AM

But i dnt want to save data in db :(

on 9/28/13, 8:39 AM

i dnt know abt "Server Action" how use that in menu click. Can you explain to me with my code example

on 9/28/13, 8:54 AM

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 9/28/13, 6:44 AM
Seen: 7784 times
Last updated: 8/25/15, 2:23 PM