Skip to Content
Menu
This question has been flagged
2 Replies
6348 Views
If I have simple computed field like this:

@api.depends('field_a', 'field_b')
def _compute_field_c(self):
for record in self:
record.field_c = field_a + field_b
Should I use for loop here? It also works if I write: self.field_c = field_a + field_b.

When should I loop through self? If I write print(record) and click on item in the list view to open form view, it only prints one record, even in for loop. So maybe I don't have to use for loop in this case?


This question also applies to inverse functions and other ones that I think are supposed to use for loop.
Avatar
Discard
Best Answer

Without Depends:
For loop should be used if your compute method doesn't have any api.depends and you have added the computing field in the list/tree view since the system will call the compute method for all the active records in the list view.

With Depends:
If you use api depends on compute method and there could be the case that those depend fields get updated at the same time, in this case for loop is needed, otherwise, you will see an error of "singleton".

But I would recommend always use for loop to avoid any traceback in the future.

Avatar
Discard
Author

Thanks. On the tutorial page I saw this example on custom actions:

from odoo import fields, models

class TestAction(models.Model):
_name = "test.action"

name = fields.Char()

def action_do_something(self):
for record in self:
record.name = "Something"
return True

Why is for loop used here? Do we need it? Because we're modifying very specific record as I understand it.

For example, there's a button "Action Something" in form view of some record. It doesn't refer to other records. We don't want to set "name" attribute for all records, only this one. I hope this makes sense. Would it be okay to do self.name = "something" in this example?

Or maybe loop is here because this action can be applied to lots of records as once?

Best Answer

With computed fields I recommend to always use for loop, you will get an error if the fields appears in list view and not using store= True. So use for loop always with computed fields.

Read more in the below link:

https://www.odoo.com/documentation/14.0/developer/reference/addons/orm.html#computed-fields

Avatar
Discard
Author

Thanks. On the tutorial page I saw this example on custom actions:

from odoo import fields, models

class TestAction(models.Model):
_name = "test.action"

name = fields.Char()

def action_do_something(self):
for record in self:
record.name = "Something"
return True

Why is for loop used here? Do we need it? Because we're modifying very specific record as I understand it.

For example, there's a button "Action Something" in form view of some record. It doesn't refer to other records. We don't want to set "name" attribute for all records, only this one. I hope this makes sense. Would it be okay to do self.name = "something" in this example?

If the action linked with button on form, you can use self.name = "something" without loop.
But sometimes you need to add this action to Actions menu as server action to apply the action to selected records from list view so you will write the server action

<record id="lunch_order_action_confirm" model="ir.actions.server">
<field name="name">Lunch: Receive meals</field>
<field name="model_id" ref="model_lunch_order"/>
<field name="binding_model_id" ref="model_lunch_order"/>
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">records.action_confirm()</field>
</record>

As you can see the action_confirm methods called for selected record from form view and if write your code of method without loop then you will get an error.

So the summary you cannot use it without loop but its recomended to use loop.

Related Posts Replies Views Activity
0
Jun 21
1886
3
Jul 20
10731
4
Oct 24
4513
0
Nov 16
3341
1
Aug 15
4090