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

Insert new record

Odebírat

Get notified when there's activity on this post

This question has been flagged
insertsql
4 Odpovědi
20242 Zobrazení
Avatar
Sebastian782

Hello,

I have 2 models A and B.

When i update field_1 on A model i execute a function that should add a new record into B model.

How can i do this?

I read that we can execute SQL request (example belaw) but maybe there is an other way more clean than do an INSERT to add new record?

query = """ SELECT event_id, state, sum(nb_register) 
            FROM event_registration
            WHERE event_id IN %s AND state IN ('draft', 'open', 'done')
            GROUP BY event_id, state """
self._cr.execute(query, (tuple(self.ids),))

Thanks,
Sebastian

1
Avatar
Zrušit
Avatar
Akhil P Sivan
Nejlepší odpověď

You can override the write function of model_A for that, so that when you are saving the record, it create a new record on model_B.

For eg, try the following, if you want to use old api in your v8 module:

from openerp.osv import fields, osv
class class_A(osv.osv):
_name = 'model.A'
_columns = {
'field_A': fields.many2one('res.partner', string="Supplier", required=True),

}

def write(self, cr, uid, ids, vals, context=None):

model_B_obj = self.pool.get('model_B')
model_B_obj.create(cr, uid, ids, {'product_id':ids, 'date':vals['status_date'], 'status':vals['status']}, context)

return super(class_A, self).write(cr, uid, ids, vals, context)

Using new api, you may try like this:

class class_A(models.Model):
_name = 'model.A'

field_A = fields.Many2one('res.partner', string="Supplier", required=True)


 
@api.multi
def write(self, vals):

model_B_obj = self.env['model_B'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status})

return super(class_A, self).write(vals)

1
Avatar
Zrušit
Sebastian782
Autor

Hi, thanks. I don't know why, i am not able to make it work. When i change my select field, it seems that it create a record, with the default values of model.B. First, I want to create record in model.B when i save model.A record, so i think the onchange is not good? It seem that it create a record when Second, i want to create the model.B with my values status_date and status of model.A Here below what i wrote: ------------------------------- @api.onchange('status') def on_change_status(self): self.env['order.history'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status}) What i am doing wrong? Thanks.

Akhil P Sivan

Hi Sebastian, I have updated the answer. You don't need to use onchange for that, just need to override the write function of model_A. Try like above.

Sebastian782
Autor

Hi Akhil, thanks for your help. I have this error : TypeError: write() takes at least 2 arguments (2 given)

Sebastian782
Autor

I use the new API

Akhil P Sivan

try without context, as updated above

Sebastian782
Autor

Perfect now Akhil, thanks :)

Avatar
james_p
Nejlepší odpověď

You can achieve it without firing SQL request. Just add the below method in model A. You can read more here: https://www.odoo.com/documentation/8.0/reference/orm.html Hope that helps.

@api.onchange('field_1')
def create_new_record(self):
self.env['model_b'].create({'field_in_model_b': value})
0
Avatar
Zrušit
Avatar
Sebastian782
Autor Nejlepší odpověď

Hi, thanks.

I don't know why, i am not able to make it work.

When i change my select field, it seems that it create a record, with the default values of model.B.

First, I want to create record in model.B when i save model.A record, so i think the onchange is not good? It seem that it create a record when

Second, i want to create the model.B with my values status_date and status of model.A

Here below what i wrote:

-------------------------------

@api.onchange('status')

def on_change_status(self):

self.env['order.history'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status})

What i am doing wrong?

Thanks.

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
Related Posts Odpovědi Zobrazení Aktivita
Usage Of SQl Queries In odoo
sql
Avatar
Avatar
1
lis 22
7458
sql constraints aren't being detected in my model
sql
Avatar
Avatar
1
říj 21
4312
Odoo on microsoft sql server instead postgres sql?
sql
Avatar
2
lis 18
11036
Odoo 7:Copy property value to new product_template field
sql
Avatar
1
bře 15
4293
Openerp with MS-SQL
sql
Avatar
Avatar
Avatar
Avatar
4
bře 15
6102
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