This question has been flagged

Can someone explain how this line is gonna work? Also the lambda function too...(code from sale.order in sale.py)

for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0):

Also if I how to sort the order_line according to the product type (service, consumable, or stockable), so that the order lines are selected according to the their type. (ie, first all the consumable products in the order_line are selected and executed, then all service type, etc....)

Avatar
Discard
Best Answer

Hi Naksha,

The sorted() function is a builtin Python function. When you execute this function it will return a sorted list of data from the given iteratable. The lambda call is an anonymous function, which has no function name. It is usually used to quickly handle looping over data and filtering something out. In this it will filter out the lines where the qty_to_invoice < 0.
You can find more information about the sorted() function in the official docs at https://docs.python.org/3/library/functions.html#sorted and more about the lambda function at https://docs.python.org/3/reference/expressions.html#lambda 

In short this line of code it will loop over all order_line values (which is a recordset) which are already sorted and only contain the values where the qty_to_invoice <= 0.

Regards,
Yenthe

Avatar
Discard