Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Using for loop when accessing/modifying fields vs using only self

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
fieldsmodelscomputed-fields
2 Besvarelser
8280 Visninger
Avatar
John Doe
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.
0
Avatar
Kassér
Avatar
Sudhir Arya (ERP Harbor Consulting Services)
Bedste svar

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.

2
Avatar
Kassér
John Doe
Forfatter

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?

Avatar
Waleed Mohsen (CorTex IT Solutions)
Bedste svar

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

1
Avatar
Kassér
John Doe
Forfatter

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?

Waleed Mohsen (CorTex IT Solutions)

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.

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
How to find the field type? Løst
fields models
Avatar
Avatar
1
jun. 25
5857
need to get the computed fields related models dinamically
models computed-fields
Avatar
0
jun. 21
3060
Design a field value verification (not odoo.api.constrains validation)
fields models
Avatar
Avatar
Avatar
3
jul. 20
12683
View a field from one model in another model Løst
fields models
Avatar
Avatar
4
okt. 24
5967
Add base field (default_code) to a model (stock.quant)
fields models
Avatar
0
nov. 16
5141
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now