Bỏ qua để đến Nội dung
Odoo Menu
  • Đăng nhập
  • Dùng thử miễn phí
  • Ứng dụng
    Tài chính
    • Kế toán
    • Hóa đơn
    • Chi phí
    • Bảng tính (BI)
    • Tài liệu
    • Ký tên
    Bán hàng
    • CRM
    • Bán hàng
    • POS Cửa hàng
    • POS Nhà hàng
    • Đăng ký
    • Cho thuê
    Trang web
    • Trình tạo trang web
    • Thương mại điện tử
    • Blog
    • Diễn đàn
    • Trò chuyện trực tiếp
    • Học trực tuyến
    Chuỗi cung ứng
    • Tồn kho
    • Sản xuất
    • PLM
    • Mua hàng
    • Bảo dưỡng
    • Chất lượng
    Nhân sự
    • Nhân viên
    • Tuyển dụng
    • Ngày nghỉ
    • Đánh giá
    • Giới thiệu
    • Đội xe
    Marketing
    • Marketing trên MXH
    • Marketing qua email
    • Marketing qua SMS
    • Sự kiện
    • Tự động hóa Marketing
    • Khảo sát
    Dịch vụ
    • Dự án
    • Bảng chấm công
    • Dịch vụ hiện trường
    • Hỗ trợ
    • Kế hoạch
    • Lịch hẹn
    Năng suất
    • Thảo luận
    • Phê duyệt
    • IoT
    • VoIP
    • Kiến thức
    • WhatsApp
    Ứng dụng của bên thứ ba Studio Odoo Nền tảng Đám mây Odoo
  • Ngành
    Bán lẻ
    • Nhà sách
    • Cửa hàng quần áo
    • Cửa hàng nội thất
    • Cửa hàng tạp hóa
    • Cửa hàng đồ kim khí
    • Cửa hàng đồ chơi
    Ẩm thực & Dịch vụ lưu trú
    • Bar và quán rượu
    • Nhà hàng
    • Đồ ăn nhanh
    • Guest house
    • Nhà phân phối đồ uống
    • Khách sạn
    Bất động sản
    • Công ty môi giới bất động sản
    • Công ty kiến trúc
    • Xây dựng
    • Quản lý bất động sản
    • Làm vườn
    • Hiệp hội chủ sở hữu bất động sản
    Tư vấn
    • Công ty kế toán
    • Đối tác Odoo
    • Công ty marketing
    • Công ty luật
    • Tuyển dụng
    • Thanh tra & chứng nhận
    Sản xuất
    • Dệt may
    • Kim loại
    • Nội thất
    • Ẩm thực
    • Nhà máy bia
    • Quà tặng doanh nghiệp
    Sức khoẻ & fitness
    • CLB thể thao
    • Cửa hàng kính mắt
    • Trung tâm fitness
    • Chuyên gia chăm sóc sức khỏe
    • Hiệu thuốc
    • Tiệm làm tóc
    Thương mại
    • Dịch vụ sửa chữa
    • Phần cứng CNTT & Hỗ trợ
    • Hệ thống năng lượng mặt trời
    • Công ty sản xuất giày
    • Dịch vụ vệ sinh
    • Dịch vụ HVAC
    Khác
    • Tổ chức phi lợi nhuận
    • Cơ quan môi trường
    • Cho thuê biển quảng cáo
    • Nhiếp ảnh
    • Cho thuê xe đạp
    • Đại lý phần mềm
    Xem tất cả ngành
  • Cộng đồng
    Học tập
    • Khóa học
    • Tài liệu
    • Chứng chỉ
    • Đào tạo
    • Blog
    • Podcast
    Thúc đẩy đào tạo
    • Chương trình đào tạo
    • Trò chơi kinh doanh Scale Up!
    • Tham quan Odoo
    Nhận phần mềm
    • Tải xuống
    • So sánh các phiên bản
    • Phiên bản
    Hợp tác
    • Github
    • Diễn đàn
    • Sự kiện
    • Dịch thuật
    • Trở thành đối tác
    • Dịch vụ dành cho Đối tác
    • Đăng ký công ty kế toán của bạn
    Nhận dịch vụ
    • Tìm đối tác
    • Tìm kế toán
    • Gặp chuyên gia hỗ trợ
    • Dịch vụ Triển khai
    • Khách hàng tham khảo
    • Hỗ trợ
    • Nâng cấp
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Nhận một buổi demo
  • Bảng giá
  • Trợ giúp

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

  • CRM
  • e-Commerce
  • Kế toán
  • Tồn kho
  • PoS
  • Dự án
  • MRP
All apps
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Hỗ trợ

Override and Change the middle of a function

Đăng ký nhận tin

Nhận thông báo khi có hoạt động trên bài viết này

Câu hỏi này đã bị gắn cờ
inheritanceodoo12
7 Trả lời
26112 Lượt xem
Ảnh đại diện
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
Ảnh đại diện
Huỷ bỏ
Janath Manthila
Tác giả

@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

Ảnh đại diện
Jake Robinson
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
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.

Ảnh đại diện
Muhammed Nishad
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
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

Ảnh đại diện
Ravi Gadhia
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Ảnh đại diện
Janath Manthila
Tác giả Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
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.

Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!

Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!

Đăng ký
Bài viết liên quan Trả lời Lượt xem Hoạt động
Function inheritance
inheritance odoo12
Ảnh đại diện
0
thg 9 20
2973
I am trying to inherit a <templates id="template" xml:space="preserve"> template, but that ate my last full week.
inheritance qweb odoo12
Ảnh đại diện
Ảnh đại diện
1
thg 6 20
13192
How to inherit one group access rights to another group in odoo12 Đã xử lý
inheritance accessrights odoo12
Ảnh đại diện
Ảnh đại diện
1
thg 8 19
7157
Can't inherit base.view_partner_form of res.partner
inheritance
Ảnh đại diện
Ảnh đại diện
Ảnh đại diện
Ảnh đại diện
Ảnh đại diện
4
thg 2 25
4181
Include in inherit from JS class
inheritance
Ảnh đại diện
Ảnh đại diện
1
thg 8 24
2980
Cộng đồng
  • Khóa học
  • Tài liệu
  • Diễn đàn
Open source
  • Tải xuống
  • Github
  • Runbot
  • Dịch thuật
Dịch vụ
  • Lưu trữ Odoo.sh
  • Hỗ trợ
  • Nâng cấp
  • Phát triển tùy chỉnh
  • Đào tạo
  • Tìm kế toán
  • Tìm đối tác
  • Trở thành đối tác
Giới thiệu công ty
  • Công ty của chúng tôi
  • Tài sản thương hiệu
  • Liên hệ
  • Việc làm
  • Sự kiện
  • Podcast
  • Blog
  • Khách hàng
  • Pháp lý • Riêng tư
  • Bảo mật
الْعَرَبيّة 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 là bộ ứng dụng kinh doanh có open source đáp ứng tất cả các nhu cầu của công ty bạn: CRM, thương mại điện tử, kế toán, tồn kho, POS, quản lý dự án, v.v.

Định hướng giá trị riêng biệt của Odoo là tích hợp hoàn toàn và dễ dàng sử dụng.

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