2

I was confused when I wrote my question, I felt that it was a vague question for some readers. So I rewrote the question again to be clear ?

I learned object oriented programming using Java. My knowledge about self in python that it's like this in Java the two are representing the instance - one instance - of the class itself. Ex: self.name, self.age, etc ...

So the question here, how Odoo framework developers made the self represents more than one instance of the class and iterate over self using traditional normal python class definition ?

for i in self

The answer is can't be achieved using normal python definition they do it using meta-class as described in URLs in the answer of this questions.

  • What is your question here ? And why do you consider the way the recordset is implemented (unordered, immutable collection) not pythonic ? What is unusual in `for record in self` ? – George Daramouskas Dec 30 '16 at 00:26
  • @GeorgeDaramouskas The question is not how to iterate over "**unordered immutable collection**", The question is how we use self as "**unordered immutable collection**" to **iterate over class instances in python**, You **can not do that using normal class** definition in python, **You have to use metaclass**. – Ahmed Kamal ELSaman Jan 01 '17 at 10:21

2 Answers2

1

Sorry to be late for posting my answer, I found my missing

Iterate over class instances (self) 01
Iterate over class instances (self) 02
Iterate over class instances (self) 03

Community
  • 1
  • 1
0

In the new api when you use the decorator @.multi odoo will call the function passing in the self a set of ids like if you check record in tree view and choose delete in the more menu odoo will pack the selected record and call the method only one time and pass all the records in a record set so self is a list you should always usel loop . But if you use @api.one decorator self will contain only one record you cannot loop the self here. So if you select multi record in the tree view and execute some action for the selected 10 record adoo will call this function 10 time every time with a record it'slike he loops throw the selected record and call the function it's not recommanded. For compute fields the decorator is @api.depends it behave like the @api.multi here self is a recordset so always use loop or youwill have a error if you didn't specially with tree view. Ihad to learn this with the hard way is used @api.depends with @api.one on the same method in a model that contains a mellion record i used print to see the work on the console odoo called the function million time soi will never use api.one onkply if i'm sure of that

Charif DZ
  • 14,415
  • 3
  • 21
  • 40