コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1788 ビュー

Extending Odoo models is quite easy but what about when we need to extend custom classes used in Odoo models ?

For example shipping providers API simple classes ?

The only way I could do that was to create a class extending shipping provider one but then I need to completely override (rewrite) entire model methods just in order to swap default shipping class with my custom instance (dependency injection)

Is there a better / easier way to do this ?

Can I somehow just swap the external class dependency inside default Odoo model without rewriting the entire function ?

Example for DHL Provider extension:

dhl_request.py (easy part)

from odoo.addons.delivery_dhl.models.dhl_request import DHLProvider

class DHLRequest(DHLProvider):
def _set_shipment_details(self, picking):
res = super()._set_shipment_details(picking)
res.Contents = "{} - {}".format(picking.sale_id.name, picking.name)
return res

delivery_dhl.py (here's the problem)

from odoo import models
from .dhl_request import DHLRequest

class DHL(models.Model):
_inherit='delivery.carrier'

def dhl_send_shipping(self, pickings):
res = []
for picking in pickings:
shipment_request = {}
# override (copy) entire function just to use our custom DHLRequest object
srm = DHLRequest(self.log_xml, request_type='ship', prod_environment=self.prod_environment)
# ... the rest of Odoo code


アバター
破棄

I think what you applied is the right approach: It's the standard python inheritance logic.
I can't think of any other simpler way.