Passa al contenuto
Odoo Menu
  • Accedi
  • Provalo gratis
  • App
    Finanze
    • Contabilità
    • Fatturazione
    • Note spese
    • Fogli di calcolo (BI)
    • Documenti
    • Firma
    Vendite
    • CRM
    • Vendite
    • Punto vendita Negozio
    • Punto vendita Ristorante
    • Abbonamenti
    • Noleggi
    Siti web
    • Configuratore sito web
    • E-commerce
    • Blog
    • Forum
    • Live chat
    • E-learning
    Supply chain
    • Magazzino
    • Produzione
    • PLM
    • Acquisti
    • Manutenzione
    • Qualità
    Risorse umane
    • Dipendenti
    • Assunzioni
    • Ferie
    • Valutazioni
    • Referral dipendenti
    • Parco veicoli
    Marketing
    • Social marketing
    • E-mail marketing
    • SMS marketing
    • Eventi
    • Marketing automation
    • Sondaggi
    Servizi
    • Progetti
    • Fogli ore
    • Assistenza sul campo
    • Helpdesk
    • Pianificazione
    • Appuntamenti
    Produttività
    • Comunicazioni
    • Approvazioni
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    App di terze parti Odoo Studio Piattaforma cloud Odoo
  • Settori
    Retail
    • Libreria
    • Negozio di abbigliamento
    • Negozio di arredamento
    • Alimentari
    • Ferramenta
    • Negozio di giocattoli
    Cibo e ospitalità
    • Bar e pub
    • Ristorante
    • Fast food
    • Pensione
    • Grossista di bevande
    • Hotel
    Agenzia immobiliare
    • Agenzia immobiliare
    • Studio di architettura
    • Edilizia
    • Gestione immobiliare
    • Impresa di giardinaggio
    • Associazione di proprietari immobiliari
    Consulenza
    • Società di contabilità
    • Partner Odoo
    • Agenzia di marketing
    • Studio legale
    • Selezione del personale
    • Audit e certificazione
    Produzione
    • Tessile
    • Metallo
    • Arredamenti
    • Alimentare
    • Birrificio
    • Ditta di regalistica aziendale
    Benessere e sport
    • Club sportivo
    • Negozio di ottica
    • Centro fitness
    • Centro benessere
    • Farmacia
    • Parrucchiere
    Commercio
    • Tuttofare
    • Hardware e assistenza IT
    • Ditta di installazione di pannelli solari
    • Calzolaio
    • Servizi di pulizia
    • Servizi di climatizzazione
    Altro
    • Organizzazione non profit
    • Ente per la tutela ambientale
    • Agenzia di cartellonistica pubblicitaria
    • Studio fotografico
    • Punto noleggio di biciclette
    • Rivenditore di software
    Carica tutti i settori
  • Community
    Apprendimento
    • Tutorial
    • Documentazione
    • Certificazioni 
    • Formazione
    • Blog
    • Podcast
    Potenzia la tua formazione
    • Programma educativo
    • Scale Up! Business Game
    • Visita Odoo
    Ottieni il software
    • Scarica
    • Versioni a confronto
    • Note di versione
    Collabora
    • Github
    • Forum
    • Eventi
    • Traduzioni
    • Diventa nostro partner
    • Servizi per partner
    • Registra la tua società di contabilità
    Ottieni servizi
    • Trova un partner
    • Trova un contabile
    • Incontra un esperto
    • Servizi di implementazione
    • Testimonianze dei clienti
    • Supporto
    • Aggiornamenti
    GitHub Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Richiedi una demo
  • Prezzi
  • Aiuto

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

  • CRM
  • e-Commerce
  • Contabilità
  • Magazzino
  • PoS
  • Progetti
  • MRP
All apps
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
Assistenza

[SOLVED] Server actions: how to replace self.write command with new API for Odoo 10?

Iscriviti

Ricevi una notifica quando c'è un'attività per questo post

La domanda è stata contrassegnata
server_actionsodoo10ir.actions.server
9 Risposte
16085 Visualizzazioni
Avatar
FEDERICO LEONI

I've started a migration to Odoo 10 from Odoo 8 and I didn't figured out how to replace a simple self.write with the new API.

For example, on one of my custom module I have

self.write(cr, uid, context['active_ids'], {'to_wait':'No'})

but when I fire the server action in my view I get

NameError: name 'self' is not defined

This is my working code for the server action on V8:

<record id="rest_order_send" model="ir.actions.server">
<field name="sequence" eval="5"/>
 <field name="name">rest_order_send</field>
 <field name="model_id" ref="model_rest_order"/>
 <field name="condition">True</field>
<field name="type">ir.actions.server</field>
 <field name="state">code</field>
 <field name="code">self.write(cr, uid, context['active_ids'],{'status':'Pronto'})
self.write(cr, uid, context['active_ids'], {'to_floor':'2'})
self.write(cr, uid, context['active_ids'], {'end_date':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")})
</field>
</record>

Looking on Odoo10 doc I found I can't use self.write  anymore and I have to use another syntax now, but sadly I didn't figured out how to use the new object_write command.


Partially solved with the help of Raaj with:

pos_obj = env['pos.order'].browse(context.get('active_id'))
pos_obj.write({'costs': '1.45'})
0
Avatar
Abbandona
Avatar
Raaj Mishra
Risposta migliore

Hi Federico, as per new API you can use like,

self.env['object_name']

for more information you can go through these document.

1.https://media.readthedocs.org/pdf/odoo-new-api-guide-line/latest/odoo-new-api-guide-line.pdf

2.https://www.odoo.com/documentation/8.0/reference/orm.html

0
Avatar
Abbandona
FEDERICO LEONI
Autore

Raaj thanks for your reply. However the first link you gave me doesn't works. I had a look again on the doc, but self still raising the same error.

self.env['pos.order']

object.write({'field': 'value'})

At this point I don't know if is possible to use self without a def on my Python code (now the action is called trough xml).

FEDERICO LEONI
Autore

Same thing with

self.env['pos.order'].write({'field': 'value'})

Raaj Mishra

Hi Federico,if you are calling the method from the pos.order object then self is enough for the operation like,

self.write({'to_wait':'No'})

But if its from different object then you need to get the id of the pos.order object,as i can see at your code in the question you are having them in context.

so in that case you need to perform like this,

pos_obj = self.env['pos.order'].browse(self._context.get('active_id'))

pos_obj.write({'field': 'value'})

if the length of active_ids is more than 1 then you can loop through the statements.

FEDERICO LEONI
Autore

Raaj, on both my module and pos.order I get always the same error for self:

ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating

u"pos_obj = self.env['pos.order'].browse(self._context.get('active_id'))\n\npos_obj.write({'costs': '1.35'})"

Raaj Mishra

Can you pls post your method for more details and the object name under which its defined.

FEDERICO LEONI
Autore

I've updated my original post with the xml pulled from one of my modules on V8.

Speaking truly, I've inherited the pos.order and pos.order.line, where I've added 2 more fields because the next step, when I'll understand how this API works, is calling a def.

That because I'm migrating part of my python scripts from my daemon inside Odoo but frankly the lack of documentation (with good examples) is discouraging. I mean a simple sql query and an update with psycopg2 took me 5 minutes of works. On Odoo I'm stuck since yesterday.

FEDERICO LEONI
Autore

Raaj,

the issue was solved after a complete remove of 'self' from the line:

pos_obj = env['pos.order'].browse(context.get('active_id'))

pos_obj.write({'costs': '1.45'})

Anyway I'm still not understanding the logic.

Avatar
Agile
Risposta migliore

In odoo10 for write  function only needed this self.write({'field name':'value'}).

0
Avatar
Abbandona
FEDERICO LEONI
Autore

In this specific example (see comments) self will rise an error and the solution I've write on the OP works fine without. So no, it's not always working.

Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!

Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!

Registrati
Post correlati Risposte Visualizzazioni Attività
Odoo 10: call a method with the new api from server action Risolto
method server_actions api.multi odoo10 ir.actions.server
Avatar
Avatar
1
feb 17
13566
Check Action Rules works only once
scheduler server_actions automatic-action odoo10
Avatar
Avatar
2
feb 17
6276
Create schedule action For sending emails
odoo10
Avatar
Avatar
Avatar
2
lug 25
6541
How to send messages that are not shown in chatter? Risolto
odoo10
Avatar
Avatar
Avatar
2
ott 25
8851
How to ORDER BY? [Odoo 10] Risolto
odoo10
Avatar
Avatar
2
nov 24
29773
Community
  • Tutorial
  • Documentazione
  • Forum
Open source
  • Scarica
  • Github
  • Runbot
  • Traduzioni
Servizi
  • Hosting Odoo.sh
  • Supporto
  • Aggiornamenti
  • Sviluppi personalizzati
  • Formazione
  • Trova un contabile
  • Trova un partner
  • Diventa nostro partner
Chi siamo
  • La nostra azienda
  • Branding
  • Contattaci
  • Lavora con noi
  • Eventi
  • Podcast
  • Blog
  • Clienti
  • Note legali • Privacy
  • Sicurezza
الْعَرَبيّة 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 è un gestionale di applicazioni aziendali open source pensato per coprire tutte le esigenze della tua azienda: CRM, Vendite, E-commerce, Magazzino, Produzione, Fatturazione elettronica, Project Management e molto altro.

Il punto di forza di Odoo è quello di offrire un ecosistema unico di app facili da usare, intuitive e completamente integrate tra loro.

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