Skip to Content
Menu
This question has been flagged
1 Reply
3405 Views

Hello !

 Here is an example code :


class myClass(model.Model):
     _name='myModule.myClass'
 
     myClassID=fields.Integer(required=True)
     att1=fields.Char(compute="_getAttr1AndAttr2", store=True)
     att2=fields.Char(compute="_getAttr1AndAttr2", store=True)
     att3=fields.Char(compute="_getAttr3",store=True)
 
     @api.one
     def _getAttr1AndAttr2(self):
         for location in self:
            self.att1="value1"
            self.att2="value2"

     @api.multi
     @api.depends('myClassID')
     def _getAttr3(self):
          for location in self:
               location.att3=('Attribute 3'+location.myClassID)
So my question is : 
- Will the compute method  _getAttr1AndAttr2(self) be called twice per record or only once at the creation of the record ?
- Will the compute method _getAttr3(self) be called only once in a tree view for example ?

I'm asking myself this because of complexity

Are there any mistakes in my code ? Like with _getAttr1AndAttr2(self) as it is @api.one do I need to set "for location in self" or is it useless ?

Thank You !
Avatar
Discard
Best Answer

- getAttr1AndAttr2 will be called twice. you can do tests by printing some debug message inside of it.  

- when a method is decorated with @api.one you don't need to iterate on self records.  "for" loop is useless here.

the compute method _getAttr3() will be called for all record that are being processed. Ex: all records being loaded in tree view. so be careful with computed fields. as it can affect perfs.

Avatar
Discard
Author

Thank you !

Author

If _getAttr3() is called for every record, what is the point using @api.multi ?

In both cases self is a collection of records :

- when api.one is used , odoo framework does the loop for you .

- when api.multi is used , you should loop on records . unless you need to work with a singleton, in this case use self.ensure_one( ) inside your method.

refer to this question https://www.odoo.com/forum/help-1/question/difference-between-api-one-and-api-multi-in-api-odoo-openerp-68209

or the Odoo technical doc http://odoo-new-api-guide-line.readthedocs.io/en/latest/decorator.html