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
    • Guest House
    • Distributeur de boissons
    • Hotel
    Real Estate
    • Real Estate Agency
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consulting
    • Accounting Firm
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Metal
    • Furnitures
    • Food
    • 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
    • Solar Energy Systems
    • Cordonnier
    • Services de nettoyage
    • HVAC Services
    Others
    • Nonprofit Organization
    • 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 keep multiple One2many fields on the same model [EDIT]

S'inscrire

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

Cette question a été signalée
one2manyrelationalduplication
2 Réponses
13604 Vues
Avatar
arthur

I opened a question yesterday (https://www.odoo.com/pt_BR/forum/help-1/question/how-to-keep-multiple-many2many-fields-on-the-same-model-145401) about how to have multiple Many2many fields. Now I am encountering the same problem with One2many fields, but can't get that right because I can't inform table_name as a relation.

My two models are the following:

class SubscriptionBasket(models.Model):
    _name="subscription.basket"
    name = fields.Char("Nome", required=True)
    date_start = fields.Date("Data Inicial", required=True)
    date_end = fields.Date("Data Final", required=True)
    sales_applied = fields.One2many(
        string=u'Vendas Aplicadas',
        comodel_name='sale.order',
        inverse_name='basket_id',
    )
    legumes = fields.One2many(
        string=u'Legumes',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    frutas = fields.One2many(
        string=u'Frutas',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    verduras = fields.One2many(
        string=u'Verduras',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    temperos = fields.One2many(
        string=u'Temperos',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )

class SubscriptionBasketItem(models.Model):
    _name = "subscription.basket.line"

    name = fields.Char("Itens da Cesta")
    basket_id = fields.Many2one(
        string=u'Cesta',
        comodel_name='subscription.basket',
    )

    product_id = fields.Many2one(
        comodel_name='product.product',
        string='Produto',
    )

    min_quantity = fields.Integer("Quantidade Mínima Disponível", default = 0)

When assigning different subscription.basket.line's to legumes fields to a subscription.basket record, for example, after creating the record all fields will have the same values, which is not the behavior I expect.  How do I get this right on One2many fields, so each field will have its own list of subscription.basket.line itens?

As an example, I overwrote the create function to get logs:

@api.model
def create(self, vals):
    _logger.info("Vals received on creation: %s", vals)
    res = super(SubscriptionBasket, self).create(vals)
    _logger.info("Field temperos.product_id on creation response: %s", res.temperos.product_d)
    return res


Then, when creating a record, informing a product just to the fields "legumes", I get the following log for the vals I received:

    Vals received on creation: {'date_end': '2019-02-06', 'name': 'Cesta', 'date_start': '2019-02-05', 'legumes': [[0,     'virtual_609', {'product_id': 1000, 'min_quantity': 0}]]}

And the following log for the field "temperos", which I would expect to be empty, on the creation response:

   Field temperos.product_id on creation response: product.product(1000,)


0
Avatar
Ignorer
Avatar
La Jayuhni Yarsyah
Meilleure réponse

Hi,,

maybe you can use related or compute fields

legumes = fields.One2many(
    string=u'Legumes',
    comodel_name='subscription.basket.line',
    inverse_name='basket_id',
)
frutas = fields.One2many(
    string=u'Frutas',
    comodel_name='subscription.basket.line',
    related="legumes",
)
verduras = fields.One2many(
    string=u'Verduras',
    comodel_name='subscription.basket.line',
    compute="_compute_value"
)
temperos = fields.One2many(
    string=u'Temperos',
    comodel_name='subscription.basket.line',
    compute="_compute_value"
)

def _compute_value(self):
    for rec in self:
        rec.temperos = rec.legumes


2
Avatar
Ignorer
arthur
Auteur

Sorry, I think that I miss explained myself. Fields containing always the same values is the current behavior, but is not what I expect. My intent is to each field to have its own list of "subscription.basket.line" itens.

La Jayuhni Yarsyah

So you must define some unique criteria to identify the record based on those fields wich related to subscription.basket.line

For ex:

class SubscriptionBasketItem(models.Model):

_name = "subscription.basket.line"

# **** Your fields

data_type = fields.Selection([('legumes','Legumes'), ('frutas','Frutas'), ('verduras','Verduras'), ('temperos','Temperos')], required=True, string="Some Type")

then you can write normal one2many fields like this in SubscriptionBasket model

legumes = fields.One2many(

string=u'Legumes',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','legumes')]

)

frutas = fields.One2many(

string=u'Frutas',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','frutas')]

)

verduras = fields.One2many(

string=u'Verduras',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','verduras')]

)

temperos = fields.One2many(

string=u'Temperos',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','temperos')]

)

and in view.xml maybe you can pass a default field context

for example

<field name="temperos" context="{'default_data_type':'temperos'}"/>

Regards

La Jayuhni Yarsyah

arthur
Auteur

I worked just as I wanted, thank you! If you past this last comment on an answer bellow, I will be glad to mark it

Avatar
Miguel Da Silva
Meilleure réponse

Hello, you are from Brazil, right? 

I to hire someone with Odoo Experience, are you interested? 

0
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é
How to prefill One2many -> Many2one ? Résolu
one2many relationship relational
Avatar
Avatar
1
sept. 17
4936
How to prefill One2many -> Many2one ?
one2many relationship relational
Avatar
0
avr. 16
9
One2many or Many2many fields Inherited model
one2many inherit many2many relational
Avatar
0
févr. 21
4420
Adding relational fields Résolu
many2one one2many relational foreignkey
Avatar
Avatar
1
avr. 18
6811
how pass value to on2many field from parent form via context
one2many
Avatar
Avatar
1
janv. 25
2687
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