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

how to format mapped() result

S'inscrire

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

Cette question a été signalée
many2manymappingodoo11
2 Réponses
12256 Vues
Avatar
andy

How to format mapped() result?

mapped() output like ['name1','name2','name3'] and related float field [100, 200, 300]

would like to add that value to fields.Text so that it goes fields.Text: name1, name2, name3

and also add currency like $100, $200, $300

can this be done? any example?

thank you

0
Avatar
Ignorer
Avatar
andy
Auteur Meilleure réponse

okey i'm partly solve:
This is my custom modules
that have this fields

report = fields.Many2many(comodel_name="my.report", string="Report")
tprod = fields.Text(string="Product", compute="_tprod")
tprice = fields.Text(string="Prices", compute="_tprice")
tcost = fields.Text(string="Cost", compute="_tcost")


@api.multi
@api.depends('report')
def _tprod(self):
        for rec in self:
            rec.tprod = str(rec.report.mapped('name')).strip("[]").strip("''")

this result is: name1', 'name2
expected : name1, name2 - with out ( ' )

@api.multi
@api.depends('report')
    def _tprice(self):
        for rec in self:
            rec.tprice = str(rec.report.mapped(lambda record: '$ ' + str(record.price))).strip("[]")

this result is : '$100', '$200'
expected: $100, $200
if i put .strip("[]").strip("''") then the result would like $100', '$200 which is not good to look at 

tcost the same as tprice

so for all of you who does not know, mapped() is a list so you cannot add .replace or .strip without turning it to str first that's why must convert str(rec.report.mapped('name')) and also record.price is Float for my part and have to change that as well to str(record.price)

if anyone have more solution to finalize my code please, thank you

Solved with:

@api.multi
@api.depends('report')
def _tprod(self):
        for rec in self:
            rec.tprod = str(rec.report.mapped('name')).strip("[]").replace("'", "")

or if it's not working, you can do this:

    rec.tprod = str(rec.report.mapped('name')).strip("[]")
    rec.tprod = rec.tprod.replace("'", "")

anyway other tips is if you use store=True then you have to delete the value first or if there is already a value in your field, then either delete the value or delete it from database (just be cautions if you are deleting from database, always have backups), or maybe just use "@api.onchange" for testing.

My problem was using store=True i did not see the .replace() because it's already saved values in database (was testing using @api.depends thought that it will change just by refreshing), choose hard way delete field from postgresql and upgrade my module - but you can also create new field and delete old ones

0
Avatar
Ignorer
Avatar
Travis Waelbroeck
Meilleure réponse

When you use `mapped`, it is going to result in a list. Afterwards, you can use standard Python to manage how you want to format that list. This isn't the prettiest example, but you can do something like this:


>>> ", ".join(["${}".format(price) for price in ['100', '200']])
'$100, $200'


1
Avatar
Ignorer
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é
Add column to a many2many rel table Résolu
many2many odoo11
Avatar
Avatar
1
nov. 19
5913
Problem with M2M Fields Résolu
many2many problem odoo11
Avatar
Avatar
Avatar
2
nov. 18
6859
Many2Many and One2Many Field in Odoo Studio Résolu
one2many many2many studio odoo11
Avatar
Avatar
Avatar
2
déc. 19
7330
Getting TypeError: Cannot read property 'documentElement' of null with fields_view_get() ODOO 11
many2many domain_filter fields_view_get odoo11
Avatar
Avatar
2
sept. 18
10256
access files from many2many-field which is passed from context
many2many context files odoo11
Avatar
0
sept. 18
4385
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