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

Create a module that can be inherited to add a common field in other custom modules

Subscriure's

Get notified when there's activity on this post

This question has been flagged
inheritanceodoo16features
3 Respostes
5350 Vistes
Avatar
ChrisB_USA

I want to add a field to several models which is the date of the weekly accounting period (weekending_date).  Originally I added this field (and a function to set the value) in each model where I required the field.  This resulted in the calculation function being duplicated in several places.

I would like to create a single module that defines the field and the function to calculate it, which I can then inherit in each model where I need it.  That way I only have the function defined once and have consistency across all models.

However, can I do this without it creating a table with just that one field in it?  

When I inherit the module, I want the field to be added into the main model where I need to use the field.  For example

Model account_move should have weekending_date added to it.

Model sale_order should have weekending_date added to it.

Any suggestions for how to do this correctly?



0
Avatar
Descartar
ChrisB_USA
Autor

Thank you. I think this is correct. I have implemented my first AbstractModel.

Avatar
Mai Chi Trung
Best Answer

I believe that is what AbstractModel is used for.

0
Avatar
Descartar
Avatar
Ashish Hirpara
Best Answer

To add a field to multiple models and define a calculation function for the field in a single module, you can create a new module and use the _inherit attribute in the module's manifest file to inherit the models that you want to add the field to.

In the new module, you can define the field and the calculation function for the field in the fields.py file. For example, you can create a fields.py file with the following content:

from odoo import fields, models

class WeekendingDate(fields.Date):
    def _compute_weekending_date(self):
        # Code to calculate the weekending date
        pass

In this code, the WeekendingDate class is a subclass of the fields.Date class, and it defines a _compute_weekending_date method that can be used to calculate the weekending date.

Next, in the module's manifest file, you can use the _inherit attribute to inherit the models that you want to add the weekending date field to, and add the weekending date field to the models using the fields.Date class. For example, you can add the following lines to the manifest file:

'_inherit': ['account.move', 'sale.order'],

'fields': {
    'weekending_date': fields.Date('Weekending Date', compute='_compute_weekending_date'),
}

In this code, the _inherit attribute is used to inherit the account.move and sale.order models, and the fields attribute is used to add the weekending_date field to the models. The weekending_date field is defined using the fields.Date class, and it has a compute attribute that specifies the _compute_weekending_date method as the calculation function for the field.

By using the _inherit attribute and the fields attribute in this way, you can add the weekending date field to multiple models in a single module, and define a calculation function for the field that will be used consistently across all the models. This will allow you to avoid duplicating the calculation function in multiple places, and ensure consistency across all the models that have the weekending date field.

0
Avatar
Descartar
ChrisB_USA
Autor

Thank you very much Ashish. This was helpful. However on attempting to implement it, I was not able to use '_inherit' and 'fields' in the manifest file. That did not work and those properties are not listed in the official documentation for the syntax and properties in the manifest file.

Possibly that was from an earlier version of odoo?

In the end I implemented this as an AbstractModel, which contains the method to calculate the weekending_date. That model is then inherited by each of the models that require the weekending date.

Avatar
Adil Akbar
Best Answer

Please check the following link for custom module:

https://youtu.be/Xya_fCNr6tw

Thanks & Regards

0
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
Odoo 16 ce: Inheritance of View to move a field from a group to another
inheritance odoo16features
Avatar
Avatar
Avatar
2
de jul. 23
4895
How to conditionally replace field on inherited document and customer preview Solved
inheritance replace odoo16features
Avatar
Avatar
1
de maig 23
4109
how to inherit form in odoo 16 Solved
form inheritance odoo16features
Avatar
Avatar
1
de març 23
4369
DeprecationWarning: The longpolling-port is a deprecated alias to the gevent-port option, please use the latter Solved
odoo16features
Avatar
Avatar
Avatar
Avatar
Avatar
5
de set. 25
24325
How to Add wizard under print button inside the form view.
odoo16features
Avatar
Avatar
Avatar
Avatar
3
d’ag. 25
3712
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