5

In a precedent question, I was looking for the BaseModel.new method to behave like the create method from ORM, but without writing changes in the database.

Does the same method exist, but acting like unlink? I would like to delete a record from my recordset in an onchange call. I don't want the record to be delete at once, but only when the user will press the Save button.

Thank you a lot in advance!


For more information about why I want this feature:

I am using the stock module of Odoo. In a Picking, I want to dynamically change the reserved products (tracked by unique serial number) which means add some new move_lines and delete some of the existing ones.

I cannot simply change the lot_id as some move_lines with this lot_id may be reserved in another Picking (or at least, I didn't find how to do this neatly).

By changing the lot_id or doing selected_move_id.move_line_ids.new({...}) in my onchange method, I can add the new ones. Now I would like to delete the existing ones that I don't need anymore. I would like to delete them the same way as if the user would have press the trash icon on the corresponding line.

mistiru
  • 2,996
  • 3
  • 11
  • 27
  • what do you mean by only when user presses `save`? – Charif DZ May 31 '19 at 14:25
  • I mean when you are editing the model containing the recordset, in the top left corner of the page, you can Edit and then Save or Discard. For example, if I edit a model, then I press the trash icon of a list, and then click the Discard button, the deleted line re-appears (it has not been deleted in database). – mistiru May 31 '19 at 14:36
  • I just edited my question, is it clearer ? – mistiru May 31 '19 at 14:38
  • You mean when you edit a `x_to_many field` ?!! – Charif DZ May 31 '19 at 14:38
  • AFAIK there is no such thing in Odoo. But I don't quite get why you'd need something like this. Could you elaborate on why you need this? – jzeta May 31 '19 at 14:39
  • Exactly, yes, like `move_line_ids` from a `StockMove`. – mistiru May 31 '19 at 14:39
  • @jzeta I want to simulate the behaviour of the trash button from a tree view, from an onchange call. – mistiru May 31 '19 at 14:40

1 Answers1

0

I tried something similar in Odoo 12 and it worked fine:

# I change the partner for testing different cases
@api.onchange('partner_id')
def onchage_p(self):
    """ remove moves have less then 5 units"""
    # create empty record set to hold valid records
    new_recs = self.env['stock.move']
    for move in self.move_ids_without_package:
        # keep only record that greater than 5
        if move.product_uom_qty > 5:
            new_recs += move

    # reassign it to the field. 
    self.move_ids_without_package = new_recs

When I debugged the write call on stock.picking the delete cammand was passed for all saved moves: [...,[2, move_id, False]]

The framework is able to detect that a record was removed from the original recordset and this is why the command was added.

So the basic Idea is create a new recordset that contains valid records assign it again to the one2many field.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • That's what I want, but this solution empties my list as it losts all the other `move_line_id` when doing the affectation. That's why I'm looking for something similar to `new` (which would be equivalent to `= [(0, '_', {...})]`. – mistiru May 31 '19 at 15:06
  • Interesting question mmm, I will try to find another solution ^^. – Charif DZ May 31 '19 at 15:08
  • you have a lot of cases too if the record is not saved, the record is saved. the record is edited ?!!! a lot – Charif DZ May 31 '19 at 15:09
  • Yes I know ^^ Thank you a lot for your time ! – mistiru May 31 '19 at 15:11
  • Could you give me the part in the code wḧich is called when we click the `trash` icon ? I cannot find it (I may be missing something trivial). Maybe it would help me understand how this works ? – mistiru May 31 '19 at 15:13
  • No when you click on trash every thing is don in the client side, it's just going to switch the command of that line to `2` or remove it. no need to contact the server for that – Charif DZ May 31 '19 at 15:16