Zum Inhalt springen
Odoo Menü
  • Anmelden
  • Jetzt gratis testen
  • Apps
    Finanzen
    • Buchhaltung
    • Rechnungsstellung
    • Spesenabrechnung
    • Tabellenkalkulation (BI)
    • Dokumente
    • E-Signatur
    Vertrieb
    • CRM
    • Vertrieb
    • Kassensystem – Shop
    • Kassensystem – Restaurant
    • Abonnements
    • Vermietung
    Websites
    • Website-Builder
    • E-Commerce
    • Blog
    • Forum
    • Livechat
    • E-Learning
    Lieferkette
    • Lager
    • Fertigung
    • PLM
    • Einkauf
    • Wartung
    • Qualität
    Personalwesen
    • Mitarbeiter
    • Personalbeschaffung
    • Abwesenheiten
    • Mitarbeiterbeurteilung
    • Personalempfehlungen
    • Fuhrpark
    Marketing
    • Social Marketing
    • E-Mail-Marketing
    • SMS-Marketing
    • Veranstaltungen
    • Marketing-Automatisierung
    • Umfragen
    Dienstleistungen
    • Projekte
    • Zeiterfassung
    • Außendienst
    • Kundendienst
    • Planung
    • Termine
    Produktivität
    • Dialog
    • Genehmigungen
    • IoT
    • VoIP
    • Wissensdatenbank
    • WhatsApp
    Apps von Drittanbietern Odoo Studio Odoo Cloud-Plattform
  • Branchen
    Einzelhandel
    • Buchladen
    • Kleidergeschäft
    • Möbelhaus
    • Lebensmittelgeschäft
    • Baumarkt
    • Spielwarengeschäft
    Essen & Gastgewerbe
    • Bar und Kneipe
    • Restaurant
    • Fast Food
    • Gästehaus
    • Getränkehändler
    • Hotel
    Immobilien
    • Immobilienagentur
    • Architekturbüro
    • Baugewerbe
    • Immobilienverwaltung
    • Gartenarbeit
    • Eigentümervereinigung
    Beratung
    • Buchhaltungsfirma
    • Odoo-Partner
    • Marketingagentur
    • Anwaltskanzlei
    • Talentakquise
    • Prüfung & Zertifizierung
    Fertigung
    • Textil
    • Metall
    • Möbel
    • Speisen
    • Brauerei
    • Firmengeschenke
    Gesundheit & Fitness
    • Sportklub
    • Brillengeschäft
    • Fitnessstudio
    • Therapeut
    • Apotheke
    • Friseursalon
    Handel
    • Handyman
    • IT-Hardware & -Support
    • Solarenergiesysteme
    • Schuster
    • Reinigungsdienstleistungen
    • HLK-Dienstleistungen
    Sonstiges
    • Gemeinnützige Organisation
    • Umweltschutzagentur
    • Plakatwandvermietung
    • Fotostudio
    • Fahrrad-Leasing
    • Software-Händler
    Alle Branchen ansehen
  • Community
    Lernen
    • Tutorials
    • Dokumentation
    • Zertifizierungen
    • Schulung
    • Blog
    • Podcast
    Bildung fördern
    • Bildungsprogramm
    • Scale-Up! Planspiel
    • Odoo besuchen
    Software anfragen
    • Herunterladen
    • Editionen vergleichen
    • Releases
    Zusammenarbeiten
    • Github
    • Forum
    • Veranstaltungen
    • Übersetzungen
    • Partner werden
    • Dienstleistungen für Partner
    • Buchhaltungsfirma registrieren
    Services anfragen
    • Partner finden
    • Buchhalter finden
    • Einen Experten treffen
    • Implementierungsservices
    • Kundenreferenzen
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Eine Demo erhalten
  • Preiskalkulation
  • Hilfe

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

  • CRM
  • e-Commerce
  • Buchhaltung
  • Lager
  • PoS
  • Projekte
  • MRP
All apps
Sie müssen registriert sein, um mit der Community zu interagieren.
Alle Beiträge Personen Abzeichen
Stichwörter (Alle anzeigen)
odoo accounting v14 pos v15
Über dieses Forum
Sie müssen registriert sein, um mit der Community zu interagieren.
Alle Beiträge Personen Abzeichen
Stichwörter (Alle anzeigen)
odoo accounting v14 pos v15
Über dieses Forum
Hilfe

Override and Change the middle of a function

Abonnieren

Erhalten Sie eine Benachrichtigung, wenn es eine Aktivität zu diesem Beitrag gibt

Diese Frage wurde gekennzeichnet
inheritanceodoo12
7 Antworten
26127 Ansichten
Avatar
Janath Manthila

Hi all,

I would like to override a function called _cart_update which is in the website_sale module. But I want to change the middle of the function (I want to do something in the middle of the function code). So I tried to rewrite the function in my custom module and add the website_sale module as a dependency module (I rewrite the function without adding any super() calls). My function gets the function call. No problem with that. But there are other modules that override the same function with super() calls. Sadly, those functions will not be executed. But I want them to be executed.

Is there any issue with my work? or is there any other way to do this?

Thank you.

3
Avatar
Verwerfen
Janath Manthila
Autor

@Jake Robinson

Thank you. Your way worked!!!. Can you provide more details on the first approach (Changing where the hook is) ? I couldn't understand what you were saying. Could you please provide an example of that??

(Sorry I don't have enough Karma to comment on your answer. :( )

Nimesh Jadav

Hello @Janath Manthila

Hope you are well.

I have the same concern. Can you please help me? If you got any solution.

Thanks

Avatar
Jake Robinson
Beste Antwort

Hi Janath

This kind of customisation is one of the trickiest parts of developing with Odoo, and poor approaches to this are one of the most common reasons I've seen apps break (especially app store apps, they love to override base code and not call super).

There are two main approaches:

Changing where the hook is: It's much better not to rewrite Odoo code, so trying another approach is always preferable. Is the code you're trying to edit actually being run in the _cart_update function, or is this function calling another function with some values that you'd like to change, or something else? If another function is being called, you can add a flag to the context before calling _cart_update, then make the change the other function that is being called if that flag exists. 

Overriding the original function: Rather than inheriting it, you can override the definition. You can do this with the following code:

from odoo.addons.website_sale.models.sale_order import SaleOrder as OriginalSaleOrder


def _cart_update(self, product_id=None, line_id=None, add_qty=0, set_qty=0, **kwargs):
# Insert the original code with your changes here

OriginalSaleOrder._cart_update = _cart_update

This just replaces the website_sale level of _cart_update, so all other modules will still function as normal, no need to call any super.

10
Avatar
Verwerfen
Yaman Alsaman

Thank you

wizardz

the problem with this is, when you uninstall the module the

´´´ OriginalSaleOrder._cart_update = _cart_update ´´´

part stays and blocks the basic function....

wizardz

So when you install or uninstall the module with the _cart_update = _cart_update override you need to restart odoo to see the real changes. Otherwise it uses the same code.

Is there any elegant way to override the middle of a basic method? This seems a little bit tricky, what if you just use the same methodname in the same class from the custom module? This will override the basic methods too.

Avatar
Muhammed Nishad
Beste Antwort

Hi Janatha,

Try this

def _cart_update(self):
    //copy the code from the function
    //add the code on the middle
    //the rest of the method you want to replace
    return super(className,self)._cart_update(self)
0
Avatar
Verwerfen
Ermin Trevisan

Please avoid this empty space.

Muhammed Nishad

@Ermin Trevisan Broh, I couldn't find any unnecessary white space in the answer, If you can find any, kindly edit and remove it, if not rollback your vote.

andy aditya

Now empty space is gone LoL

Avatar
Ravi Gadhia
Beste Antwort

Is there any issue with my work? 
   No there is no issue as long as your module dependency tree is intact/clear.  ​
is there any other way to do this?
   No there no way to override something which is in the middle of the method           

0
Avatar
Verwerfen
Avatar
Janath Manthila
Autor Beste Antwort

Hi Muhammed 

Thank you for the reply.

But will it be a double code execution because we rewrite the code and call the parent function by adding super()?

Hi Ravi,

Thank you for the reply. Do you have any thoughts on why it doesn't execute the other functions which are written in other modules (using super())?

0
Avatar
Verwerfen
Muhammed Nishad

that's true brother, let me check if there is any way to walk around it.

Ravi Gadhia

it's because you do not call super in your method so it will not execute other functions. in this case you may change the module dependency it's will change MRO (Method Resolution Order) so it will call first other methods then your method.

Diskutieren Sie gerne? Treten Sie bei, statt nur zu lesen!

Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!

Registrieren
Verknüpfte Beiträge Antworten Ansichten Aktivität
Function inheritance
inheritance odoo12
Avatar
0
Sept. 20
2980
I am trying to inherit a <templates id="template" xml:space="preserve"> template, but that ate my last full week.
inheritance qweb odoo12
Avatar
Avatar
1
Juni 20
13211
How to inherit one group access rights to another group in odoo12 Gelöst
inheritance accessrights odoo12
Avatar
Avatar
1
Aug. 19
7161
Can't inherit base.view_partner_form of res.partner
inheritance
Avatar
Avatar
Avatar
Avatar
Avatar
4
Feb. 25
4196
Include in inherit from JS class
inheritance
Avatar
Avatar
1
Aug. 24
2984
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Herunterladen
  • Github
  • Runbot
  • Übersetzungen
Dienstleistungen
  • Odoo.sh-Hosting
  • Support
  • Upgrade
  • Individuelle Entwicklungen
  • Bildung
  • Buchhalter finden
  • Partner finden
  • Partner werden
Über uns
  • Unsere Firma
  • Markenwerte
  • Kontakt
  • Karriere
  • Veranstaltungen
  • Podcast
  • Blog
  • Kunden
  • Rechtliches • Datenschutz
  • Sicherheit
الْعَرَبيّة 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 ist eine Suite von Open-Source-Betriebsanwendungen, die alle Bedürfnisse Ihres Unternehmens abdecken: CRM, E-Commerce, Buchhaltung, Lager, Kassensystem, Projektmanagement etc.

Das einzigartige Wertversprechen von Odoo ist, dass es gleichzeitig sehr einfach zu bedienen und voll integriert ist.

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