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
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • 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
    Procházet všechna odvětví
  • 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
    • Služby pro partnery
    • 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

Is there a way to set a value for a field for all existing records in the database at addon installation only?

Odebírat

Get notified when there's activity on this post

This question has been flagged
5 Odpovědi
10057 Zobrazení
Avatar
Michael Karrer

Situation:

  1. I have an existing Database with the model event.calendar.

  2. In this Database there are already a lot of records for calendar.event

  3. I install a new addon that extends the event.calendar model with a new field "category" with required=TRUE

RESULT / ERROR:

Since the existing records do not hold any value for the new field the installation of the addon fails. Because the NOT NULL constrain could not be created since there are already records with NULL values for the new field. (Chicken Egg problem in some way)

QUESTION:

Is there a way to set a default value for this new field but just for all existing records in the Database at module (addon) installation?


Hint:

I do not want to set a permanent _default parameter for this field since after the installation is done the user should carefully set this field to a meaningful value.

3
Avatar
Zrušit
Avatar
Marvin Taboada
Nejlepší odpověď

Michael, `_auto_init()` is an ORM method not expected to be completely redefined because it does important stuff during module initialization, please see:

https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L2416 

You can safely override this method provided you also invoke the parent's class implementation as it is done in several models from the base module, e.g.:

https://github.com/odoo/odoo/blob/8.0/openerp/addons/base/ir/ir_attachment.py#L217

    def _auto_init(self, cr, context=None):
        super(ir_attachment, self)._auto_init(cr, context)
        # Now safely perform your own stuff
        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_attachment_res_idx',))
...

The method I prefer for these cases is to add an `init(self, cr)` method on models. This method is invoked after `_auto_init()` provided it is defined in the model, see the following link for details about how both methods are invoked:

https://github.com/odoo/odoo/blob/8.0/openerp/modules/module.py#L274 

Have a nice day.

2
Avatar
Zrušit
Michael Karrer
Autor

Thank you very much!

Avatar
Emipro Technologies Pvt. Ltd.
Nejlepší odpověď

Hi,

To achieve your goal you need to define one method in your class where you add that new field. And set data into existing records. Please have a look on the below code.

class calendar_event(...)
    _inherit = "event.calendar"
    category_id = Fields.many2one(....)

    def _auto_init(self, cr, context=None):
        cr.execute("update event_calendar set category_id = 1 where category_id is null")

I hope it will resolve your issue.

Thanks.

4
Avatar
Zrušit
Michael Karrer
Autor

Thank you! This was the method i was searching for!

Avatar
Jérôme Thériault
Nejlepší odpověď

Old question but still interresting for newer versions. In 12.0 for example,  _auto_init() and init() are fine for indexes but for NOT NULL constrains, by the time those methods are executed, it's too late and the SQL constraints have already been applied and caused a NOT NULL constraint error due to existing rows with null data. It seems the way to handle this properly is by overriding the _init_column() method, check for the column name and do the SQL UPDATE statements in there:

    @api.model_cr_context
    def _init_column(self, column_name):
        # Set a value on existing data for required fields
        if column_name == 'real_check_in':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_in = check_in WHERE real_check_in IS NULL")
        elif column_name == 'real_check_out':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_out = check_out WHERE real_check_out IS NULL")
        super()._init_column(column_name)


0
Avatar
Zrušit
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
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