What is Monkey Patching & How It Can Be Applied in Odoo 17

SUPRIYA
June 6, 2024
what-is-monkey-Patching

Monkey patching is a technique that allows you to change or extend the behaviour of existing code at runtime. This approach makes it easier to make changes to the code without having to modify the source code itself. This can prove valuable in scenarios where you need to introduce new features, fix bugs, or overwrite existing code without directly modifying the source code.

Using a methodical process, we apply monkey patching.

assign new_method to MyClass.original_method it replaces the code with the new method, so the original method is called the new method will be executed, making your modifications, without you having to modify the original method itself.

Example: Splitting Deliveries in Sale Orders

Consider a sales order scenario where the goal is to split up a delivery for each product listed on the order. A single delivery called a pick, for the entire sales order containing all products. To do this, you must insert fields into the order lines that indicate the delivery dates for each product. and you must modify the _assign_picking method of the stock.move model. This method creates a pick for each move. Transfer the field and introduce it to the order line. To facilitate splitting the delivery based on the delivery date, it is important to include the delivery date field on the sales order line.

class SaleOrderlineInherit(models.Model):

class-sale-orderline-inherit

Incorporate the field into the views.

 what is monkey patching

adjusting the procedure for “prepare procurement values.”

splitting shipments, the delivery date scheduled ought to coincide with the date that each product’s order line specifies. Monkey patching must be used on the sale order line model’s _prepare_procurement_values method in order to achieve this.

 what is monkey patching

match the delivery date mentioned in the relevant order line, we have changed the date_deadline value in this case. The customer lead time determines the order date delivery date is provided.

Next, we assign the recently developed method to the SaleOrderLine class.

SaleOrderLine._prepare_values_for_procurement = _prepare_values_for_procurement

Inheriting the Stock Move Model

we extend the functionality of the stock move model by inheriting it using the _inherit attribute

inheriting-using the-inherit attribute

Adjusting the _assign_picking method.

we modify the _assign_picking method to accomplish the intended splitting of deliveries.

adjusting-assign-picking-method

Let’s explain the code.

pickings = self.env[‘stock.picking’]

we define a variable named picking as an empty record of the ‘stock.picking’ model.

grouped_moves = groupby(self, key=lambda m: m._key_assign_picking())

for group, moves in grouped_moves:

moves = self.env['stock.move'].concat(*moves)

The grouped_moves variable is generated using the groupby function, which groups the moves based on a key derived from the _key_assign_picking of each move. it iterates through each move group and concatenates them.

pickings = moves[0]._search_picking_for_assignation()

by utilizing the first move, it searches for a picking by employing the _search_picking_for_assignation method.

search-picking-for-assignation method

Partner_id and origin are updated if a “pickings” is found and there are no “pickings,” the code

picking-code

looks for bad moves and eliminates them.

After filtering, if any moves remain, a new picking using the picking values obtained

get-new-picking-values

from the _get_new_picking_values function.

Each move generates a distinct picking.

StockMove._assign_picking = _assign_picking

The StockMove class’s _assign_picking method the newly created function. We have effectively achieved the desired functionality of dividing deliveries within the sale order thanks to these applied monkey patches. It is wise to investigate other options that seem appropriate. This summarizes how to use Odoo 17’s monkey patching feature.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *