Skip to Content
Menu
This question has been flagged
2 Replies
1037 Views

Dear all

My codes are, how to write this inverse function so the user can edit the field 'lot_ids'. thanks


lot_ids = fields.Many2many('stock.lot', string='Lots/Serials', compute='_get_lot_name',
readonly=False, domain="[('product_id', '=', product_id)]", store=True)
lot_name = fields.Char(string='Lots/Serials', compute='_get_lot_name', readonly=False, store=True)
has_lot_id = fields.Boolean(string='Has Lot ids', compute="_compute_has_lot_id", store=True)
@api.depends('has_lot_id', 'picking_id','picking_id.move_line_ids')
def _get_lot_name(self):
for rec in self:
if rec.has_lot_id and rec.picking_id:
stock_move_lines = self.env['stock.move.line'].sudo().search([('picking_id', '=', rec.picking_id.id)])
# print('stock_move_lines', stock_move_lines, stock_move_lines.mapped('lot_id'),
# stock_move_lines.mapped('lot_name'))
if all(value is not False for value in stock_move_lines.mapped('lot_name')):
# Get the lot names from the stock move lines, filtering out any non-string values (e.g.,False)
# print('stock_move_lines.mapped name', stock_move_lines.mapped('lot_name'))
lot_names = [name for name in stock_move_lines.mapped('lot_name') if isinstance(name, str)]
rec.lot_name = ', '.join(lot_names) if lot_names else ''
elif len(stock_move_lines.mapped('lot_id').mapped('id')):
# print('stock_move_lines.mapped', stock_move_lines.mapped('lot_id').mapped('id'))
lot_ids = stock_move_lines.mapped('lot_id').ids
rec.lot_ids = self.env['stock.lot'].browse(lot_ids)







Avatar
Discard
Best Answer

Hi,
In the inverse function you have to set the reverse logic you have added inside the compute function. For reference have a look at this video:  How To Set Inverse Function For Computed Field In Odoo


Thanks

Avatar
Discard
Best Answer

Hi,

To write an inverse function for a computed field in Odoo 17, refer to the blog How to add inverse function for compute fields.


The blog provides a detailed example of how to define and implement such an inverse function.


Hopes it helps.

Avatar
Discard
Related Posts Replies Views Activity
4
May 25
2110
2
May 25
5458
1
Mar 25
1365
4
Mar 25
4139
3
Feb 25
5081