Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

Why to add Many2One field on both models?

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
many2oneone2manyrelationfield
1 Beantwoorden
10509 Weergaven
Avatar
Christian mahlich

Dear all,

I am doing a code review of some of our custom modules and found the following models which habe a many2One relation from both models. 
Could you please explain me if this can be correct, and if so what logic behind this is? I assumed the models are linked with each other if I add a relation filed on one side.

class product_template(models.Model):
_inherit="product.template"
handling_group_id = fields.Many2one('handling.group', string='Handling Group')

class HandlingGroup(models.Model):
    _name='handling.group'
    _rec_name = 'handle_grp'
    name = fields.Char(required=True)
    product_id = fields.Many2one("product.product", string="Product ID")


Next I found was a relation on the one model as Many2Many and on the other as Many2One relation. 

class product_template(models.Model):
_inherit="product.template"
    branding_ids = fields.Many2many('branding.item', 'branding_item_rel', 'product_id', 'brand_id', string="Branding Items")


class BrandingPosition(models.Model):
    _name = 'branding.position'
    _description = 'Branding'
    _order = "name"
    product_id = fields.Many2one('product.product', string="SKU", required=True)


Hope you can explain to me what the sense of this is. And why I would implement it like this.
Thanks in advance!

 

0
Avatar
Annuleer
Sehrish

You must learn odoo customization: https://plus.google.com/collection/MrdEWE

Avatar
Caret IT Solutions Pvt. Ltd.
Beste antwoord

Hi,

In your code, you can see handling.group has relation with product.product not with product.template and 

It is not compulsory to add many2one in both models when you want to establish a relationship with 2 models.

You can use only one many2one field to use that model data in another model.

In a simple way,

To use Many2one you only need to define comodel name parameter in a field:

customer_id = fields.Many2one('res.partner')


To use One2many you need to define comodel name with inverse field name:

for ex:- To add custom model One2many record in res.partner:

Class Partner(models.Model):

    _inherit = 'res.partner'

    my_model_ids = fields.One2many('my.model', 'partner_id')


Class Mymode(models.Model):

        _name = 'my.model'

        partner_id = fields.Many2one('res.partner')

For many2many :


Class Mymode(models.Model):

        _name = 'my.model'

        partner_ids = fields.Many2many('res.partner')     

You can add optional name of the column referring to "these" in the table ``relation

To get more details about using these fields you can refer the comment details available in the file : 

odoo > fields.py

Class Many2one

Class One2many

Class Many2many

To get examples you can see documentation of Odoo:

https://www.odoo.com/documentation/12.0/howtos/backend.html#relational-fields



0
Avatar
Annuleer
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
How do I put One2many after Many2one defined? - transient module states reset
many2one one2many
Avatar
1
mrt. 23
3210
Odoo Studio how to set a chain of multiple and related Many2one within a One2many lines
many2one one2many
Avatar
0
dec. 22
3808
filter according to comodel's fields
many2one one2many
Avatar
0
jun. 21
3514
How to display other fields of a onetomany relation ?
many2one one2many
Avatar
0
jun. 20
6371
One2many or many2one Opgelost
many2one one2many
Avatar
Avatar
1
nov. 19
3271
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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