3

This is the problem, which happens to everyone who needs to use active_id: https://github.com/odoo/odoo/issues/39070

In summary, in a customized stock.production.lot view, I have this:

<field name="outgoing_moves" context="{'default_lot_id': active_id}"/>

Where outgoing_moves is a One2many field. I only want to fill in automatically the field lot_id with the current opened lot each time the user adds a new outgoing move.

The problem is that if I open this lot view from other model view (through a shortcut button for example), the active_id is not updated, and brings me the ID of the previous model.

Example, I open the lot 61 after clicking on a button in the customer 133. When I try to add a new outgoing move in the One2many field, I get the error "The lot with ID 133 does not exist". This is an Odoo bug of the 13.0 version, currently not fixed yet.

The solution they propose in the GitHub thread is to use active_ids[0] instead of active_id:

Notice that is does use active_ids[0], which works even in the example you provided.

<field name="outgoing_moves" context="{'default_lot_id': active_ids[0]}"/>

Well, everyone seems to be happy with that solution, but it is not working in my case. I am pretty sure that my code is right updated and that I am using active_ids[0], but I always get the wrong ID.

Another solution I've read is to use the realiable ID, which means, the ID of the record (lot in my case) itself. I've tried and worked, but sometimes, after restarting Odoo, I get a JavaScript error each time I click on whatever in Odoo. And the error disappears when I remove the context of outgoing_moves:

<field name="id" invisible="1"/>
<field name="outgoing_moves" context="{'default_lot_id': id}"/>

Does anyone experienced this problem and found a workaround?

forvas
  • 9,801
  • 7
  • 62
  • 158
  • 1
    `lot_id` is the inverse field for the One2many field if i'm guessing right? Why do you even need the field? It will be set by Odoo on creation, just remove it from the view, or if it is a default view (used elsewhere) make a inherited view which removes the field, and use this in the lot form view for `outgoing_moves` field. – CZoellner Nov 06 '20 at 12:39
  • 1
    The inherited view has to be a primary view, which will not extend the original view but inherit from it only. – CZoellner Nov 06 '20 at 12:41
  • Yes, it is the inverse field and you are right, if I remove it from the view it is filled in automatically by Odoo on creation. In my case, I do not have to inherit the view since I only use it here. So your comment is the solution to my problem, thank you very much. You can convert it into the answer so that I can accept it. However, the Odoo `active_id` issue is still a problem for future developments, have you experienced that? – forvas Nov 06 '20 at 13:02

1 Answers1

2

lot_id is the inverse field for the One2many field if i'm guessing right?

You don't need to show or even define this field on the list/form view of the One2many field's model, because it will be filled by Odoo automatically on creation.

But if it is a default view (used elsewhere) make a inherited view which removes the field and define this new view as used view especially where you need this.

This is not a solution for the active_id problem. I don't know of one yet.

CZoellner
  • 13,553
  • 3
  • 25
  • 38