Se rendre au contenu
Odoo Menu
  • Se connecter
  • Essai gratuit
  • Applications
    Finance
    • Comptabilité
    • Facturation
    • Notes de frais
    • Feuilles de calcul (BI)
    • Documents
    • Signature
    Ventes
    • CRM
    • Ventes
    • PdV Boutique
    • PdV Restaurant
    • Abonnements
    • Location
    Sites web
    • Site Web
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Chaîne d'approvisionnement
    • Inventaire
    • Fabrication
    • PLM
    • Achats
    • Maintenance
    • Qualité
    Ressources Humaines
    • Employés
    • Recrutement
    • Congés
    • Évaluations
    • Recommandations
    • Parc automobile
    Marketing
    • Marketing Social
    • E-mail Marketing
    • SMS Marketing
    • Événements
    • Marketing Automation
    • Sondages
    Services
    • Projet
    • Feuilles de temps
    • Services sur Site
    • Assistance
    • Planification
    • Rendez-vous
    Productivité
    • Discussion
    • Validations
    • Internet des Objets
    • VoIP
    • Connaissances
    • WhatsApp
    Applications tierces Odoo Studio Plateforme Cloud d'Odoo
  • Industries
    Commerce de détail
    • Librairie
    • Magasin de vêtements
    • Magasin de meubles
    • Épicerie
    • Quincaillerie
    • Magasin de jouets
    Food & Hospitality
    • Bar et Pub
    • Restaurant
    • Fast-food
    • Maison d’hôtes
    • Distributeur de boissons
    • Hôtel
    Immobilier
    • Agence immobilière
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consultance
    • Cabinet d'expertise comptable
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Métal
    • Meubles
    • Alimentation
    • Brewery
    • Cadeaux d'entreprise
    Santé & Fitness
    • Club de sports
    • Opticien
    • Salle de fitness
    • Praticiens bien-être
    • Pharmacie
    • Salon de coiffure
    Trades
    • Bricoleur
    • Matériel informatique et support
    • Systèmes photovoltaïques
    • Cordonnier
    • Services de nettoyage
    • Services CVC
    Autres
    • Organisation à but non lucratif
    • Agence environnementale
    • Location de panneaux d'affichage
    • Photographie
    • Leasing de vélos
    • Revendeur de logiciel
    Browse all Industries
  • Communauté
    Apprenez
    • Tutoriels
    • Documentation
    • Certifications
    • Formation
    • Blog
    • Podcast
    Renforcer l'éducation
    • Programme éducatif
    • Business Game Scale-Up!
    • Rendez-nous visite
    Obtenir le logiciel
    • Téléchargement
    • Comparez les éditions
    • Versions
    Collaborer
    • Github
    • Forum
    • Événements
    • Traductions
    • Devenez partenaire
    • Services for Partners
    • Enregistrer votre cabinet comptable
    Nos Services
    • Trouver un partenaire
    • Trouver un comptable
    • Rencontrer un conseiller
    • Services de mise en œuvre
    • Références clients
    • Assistance
    • Mises à niveau
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obtenir une démonstration
  • Tarification
  • Aide

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

  • CRM
  • e-Commerce
  • Comptabilité
  • Inventaire
  • PoS
  • Projet
  • MRP
All apps
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Aide

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

S'inscrire

Recevez une notification lorsqu'il y a de l'activité sur ce poste

Cette question a été signalée
server_actionsodoo10ir.actions.server
9 Réponses
16084 Vues
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
Ignorer
Avatar
Raaj Mishra
Meilleure réponse

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
Ignorer
FEDERICO LEONI
Auteur

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
Auteur

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
Auteur

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
Auteur

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
Auteur

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
Meilleure réponse

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

0
Avatar
Ignorer
FEDERICO LEONI
Auteur

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.

Vous appréciez la discussion ? Ne vous contentez pas de lire, rejoignez-nous !

Créez un compte dès aujourd'hui pour profiter de fonctionnalités exclusives et échanger avec notre formidable communauté !

S'inscrire
Publications associées Réponses Vues Activité
Odoo 10: call a method with the new api from server action Résolu
method server_actions api.multi odoo10 ir.actions.server
Avatar
Avatar
1
févr. 17
13566
Check Action Rules works only once
scheduler server_actions automatic-action odoo10
Avatar
Avatar
2
févr. 17
6276
Create schedule action For sending emails
odoo10
Avatar
Avatar
Avatar
2
juil. 25
6541
How to send messages that are not shown in chatter? Résolu
odoo10
Avatar
Avatar
Avatar
2
oct. 25
8851
How to ORDER BY? [Odoo 10] Résolu
odoo10
Avatar
Avatar
2
nov. 24
29773
Communauté
  • Tutoriels
  • Documentation
  • Forum
Open Source
  • Téléchargement
  • Github
  • Runbot
  • Traductions
Services
  • Hébergement Odoo.sh
  • Assistance
  • Migration
  • Développements personnalisés
  • Éducation
  • Trouver un comptable
  • Trouver un partenaire
  • Devenez partenaire
À propos
  • Notre société
  • Actifs de la marque
  • Contactez-nous
  • Emplois
  • Événements
  • Podcast
  • Blog
  • Clients
  • Informations légales • Confidentialité
  • Sécurité.
الْعَرَبيّة 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 est une suite d'applications open source couvrant tous les besoins de votre entreprise : CRM, eCommerce, Comptabilité, Inventaire, Point de Vente, Gestion de Projet, etc.

Le positionnement unique d'Odoo est d'être à la fois très facile à utiliser et totalement intégré.

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