Skip to Content
Menu
This question has been flagged
4973 Views

i am beginner in learning odoo 10 , i start learnning from Odoo 10 Development essentials book i came across this simple example in page 46,  where all it has to do is looping through record of field and change it's value from True to False or the opposite 

the view:

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

  <field name="name">To-do Task Form</field>

  <field name="model">todo.task</field>

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

   <form string="To-do Task">

    <header>

    <button name="do_toggle_done" type="object"

     string="Toggle Done" class="oe_highlight" />

    </header>

    <sheet>

     <group name="group_top">

      <group name="group_left">

       <field name="name"/>

      </group>

      <group name="group_right">

       <field name="is_done"/>

       <field name="active" readonly="1"/>

      </group>

     </group>

    </sheet>

   </form>

  </field>

 </record>

--------------------------


the module :

# -*- coding: utf-8 -*-


from odoo import models, fields, api


class TodoTask(models.Model):

 _name = 'todo.task'

 _description = 'To-do Task'

 name = fields.Char('Description', required=True)

 is_done = fields.Boolean('Done?')

 active = fields.Boolean('Active?', default=True)

 @api.multi

 def do_toggle_done(self):

  for task in self:

   # print task.is_done, self

   task.is_done = not task.is_done

  return True


------------------------------


and the problem happen in the do_toggle_done method it should loop over all the model record but self in here only give me the current record i deal , with even when i use  @api.multi decorator , self only give me the current record .

is't looping through self will give me all the records in the recordset ?


update:


calling the method from the button still give the recordset in self the current record but if i call self.env['todo.task'].search([]) inside this method i can get all all the record in the model,  so why calling the method from the button give it the current record and not all the records in the model .

@api.multi

 def do_toggle_done(self):

  records = self.env['todo.task'].search([])

  print len(records), records

  for task in records:

   task.is_done = not task.is_done

  return True


Avatar
Discard