Odoo Help

0

Calling a function from function, why is result in square brackets?

By
Lucas Huber
on 3/15/16, 10:17 AM 517 views

I don't now why the result of a function from another function seems to be in Square brackets:

time event 1:  ['2016-02-28 10:10:28'] 
time event 2: 2016-02-28 10:10:28

Time event 1 code (call):

dummy = self._compute_stop_time_hrs(self.start_datetime, self.duration)
print "time event 1: ", dummy

Time event 2 code (shortend):

@api.one  # TODO causes error "strftime" Getting the Stop DateTime
def _compute_stop_time_hrs(self, start, duration):
return self.start_datetime

Seems that I miss something basic here?

How did you declared start_datetime ?

Motez
on 3/15/16, 10:43 AM
2

Temur

--Temur--
2932
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 3/15/16, 11:13 AM

It's a completely expected result with @api.one

@api.one is decorator, that may used instead of @api.multi, if you want to iterate automatically over "ids" (that is recordset in v8.0+) passed to the function. If called function returns anything, then @api.one collects results from each call of function into the list, and finally that list is returned. 

So, if you'r function do not return anything, then you're free to use @api.one without difficulties, but if your function return something, then you'll need to consider the above described behavior and if the resulting list does not fit into your requirements, then fall back to the @api.multi and use it. if you want to make sure and check additionally that "self" contains exactly one record, then you can check it with ensure_one() function:

    @api.multi
def _compute_stop_time_hrs(self, start, duration): self.ensure_one() #One record expected, raise error if self is an unexpected recordset
return self.start_datetime



Thanks @Temur now it gets clear!

Lucas Huber
on 3/15/16, 11:26 AM

@Temur Nicely put. @api.one usually does not do what you want it to do; it's mostly there for compatibility with the old api... It duplicates the environement for every record in the set (really bad for perfs), returns a list, etc. In most cases it's not what you need (even though you think you do).

Damien Bouvy (dbo)
on 3/16/16, 5:35 PM

Nice note about performance, Thanks

Temur
on 3/31/16, 10:52 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.

Register

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

1 follower(s)

Stats

Asked: 3/15/16, 10:17 AM
Seen: 517 times
Last updated: 3/15/16, 11:26 AM