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

Defining a count function based on a Many2many field

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
pythonmany2manyodootables
2 Antwoorden
7204 Weergaven
Avatar
Raib

Hi, I've created a custom module in which I added these three models named "dipendenti.py" (just consider it such as "students" to simplify things), "corso.py" and "edizione.py" (in Italian, "corso" is a word used to refer to a course in general, such as it would be literature, and "edizione" is a word used to refer to a specific course that has been holded, such as it would be literature in XX-HighSchool in 9th grade in the 2020-21 academic year). I'll write down the important code lines to contextualize:


DIPENDENTE.PY
class Dipendente(models.Model):
_name="piani_formazione.dipendente"
[...]codice_fiscale = fields.Char(string="Codice Fiscale", size=16, required=True)
dipendente_edizioni = fields.Many2many("piani_formazione.edizione","tabella_dipendenti_edizioni","codice_fiscale","numero",string="Edizioni frequentate")
CORSO.PY
class Corso(models.Model):
_name ="piani_formazione.corso"
[...]codice = fields.Char(string="Codice", required=True)
edizioni_corso = fields.One2many("piani_formazione.edizione","edizione_corso", string="Edizioni erogate")
EDIZIONE.PY
class EdizioneCorso(models.Model):
_name="piani_formazione.edizione"
[...]numero=fields.Text(string="Numero",required=True)
edizione_corso = fields.Many2one("piani_formazione.corso",string="Edizione del corso",required=True)
edizione_dipendenti = fields.Many2many("piani_formazione.dipendente","tabella_dipendenti_edizioni","numero","codice_fiscale",string="Frequentanti")

Now I would like to add to the "edizione.py" a computed field, that for each record ("for each edizione") registers how many dipendenti ("students") have attended that edizione ("specific course"). But I don't know how to count such quantity from tables, since the Many2many relation generates tables  instead of lists. 

0
Avatar
Annuleer
Raib
Auteur

@samuel I tried, but, since the relation creates a table and not a list, "len()" seems to not work on it

Avatar
Raib
Auteur Beste antwoord

@samuel I found out that len() was not the problem, I just indented wrong the method! Thank you :)

0
Avatar
Annuleer
Samuel RAMAROSELY

You're welcome

Avatar
Samuel RAMAROSELY
Beste antwoord

Hello Raib,

Try this:

Let's assume that you created your computed field like this:

dipendenti_number = fields.Integer(compute='_compute_dipendenti_number')

Now in your compute function

@api.depends('edizione_dipendenti')
def _compute_dipendenti_number(self):
    for rec in self:
        rec.dipendenti_number = len(rec.edizione_dipendenti)



-1
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
Save filtered tree view to load as it is at another time Opgelost
python odoo
Avatar
Avatar
Avatar
2
aug. 25
3393
Private functions and public functions in odoo python Opgelost
python odoo
Avatar
Avatar
Avatar
Avatar
3
feb. 25
4797
odoo ghost module
python odoo
Avatar
0
mei 24
46
Call python method from inherit_id attribute
python odoo
Avatar
Avatar
1
apr. 24
4180
how to get last inserted value from One2many or many2many field Opgelost
python one2many many2many odoo v15
Avatar
Avatar
Avatar
2
dec. 23
2812
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