Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
1128 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Paras vastaus

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
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
4
toukok. 25
2593
2
toukok. 25
5990
1
maalisk. 25
1723
4
maalisk. 25
4586
3
helmik. 25
5594