Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textile
    • Kov
    • Nábytek
    • Jídlo
    • Brewery
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • IT hardware a podpora
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Browse all Industries
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Services for Partners
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

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

Odebírat

Get notified when there's activity on this post

This question has been flagged
fieldsmodelscomputed-fields
2 Odpovědi
8341 Zobrazení
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
Zrušit
Avatar
Sudhir Arya (ERP Harbor Consulting Services)
Nejlepší odpověď

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
Zrušit
John Doe
Autor

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)
Nejlepší odpověď

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
Zrušit
John Doe
Autor

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!

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
View a field from one model in another model Vyřešeno
fields models
Avatar
Avatar
1
lis 25
6049
How to find the field type? Vyřešeno
fields models
Avatar
Avatar
1
čvn 25
5900
need to get the computed fields related models dinamically
models computed-fields
Avatar
0
čvn 21
3067
Design a field value verification (not odoo.api.constrains validation)
fields models
Avatar
Avatar
Avatar
3
čvc 20
12716
Add base field (default_code) to a model (stock.quant)
fields models
Avatar
0
lis 16
5199
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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