Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Procházet všechna odvětví
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Služby pro partnery
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

Override and Change the middle of a function

Odebírat

Get notified when there's activity on this post

This question has been flagged
inheritanceodoo12
7 Odpovědi
26118 Zobrazení
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
Zrušit
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
Nejlepší odpověď

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
Zrušit
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
Nejlepší odpověď

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
Zrušit
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
Nejlepší odpověď

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
Zrušit
Avatar
Janath Manthila
Autor Nejlepší odpověď

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
Zrušit
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.

Enjoying the discussion? Don't just read, join in!

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

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
Function inheritance
inheritance odoo12
Avatar
0
zář 20
2978
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
čvn 20
13201
How to inherit one group access rights to another group in odoo12 Vyřešeno
inheritance accessrights odoo12
Avatar
Avatar
1
srp 19
7158
Can't inherit base.view_partner_form of res.partner
inheritance
Avatar
Avatar
Avatar
Avatar
Avatar
4
úno 25
4190
Include in inherit from JS class
inheritance
Avatar
Avatar
1
srp 24
2983
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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