Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

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

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Defining a count function based on a Many2many field

Subscriure's

Get notified when there's activity on this post

This question has been flagged
pythonmany2manyodootables
2 Respostes
7210 Vistes
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
Descartar
Raib
Autor

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

Avatar
Raib
Autor Best Answer

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

0
Avatar
Descartar
Samuel RAMAROSELY

You're welcome

Avatar
Samuel RAMAROSELY
Best Answer

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
Descartar
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
Save filtered tree view to load as it is at another time Solved
python odoo
Avatar
Avatar
Avatar
2
d’ag. 25
3401
Private functions and public functions in odoo python Solved
python odoo
Avatar
Avatar
Avatar
Avatar
3
de febr. 25
4809
odoo ghost module
python odoo
Avatar
0
de maig 24
46
Call python method from inherit_id attribute
python odoo
Avatar
Avatar
1
d’abr. 24
4188
how to get last inserted value from One2many or many2many field Solved
python one2many many2many odoo v15
Avatar
Avatar
Avatar
2
de des. 23
2819
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة 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 és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

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