Search code examples
pythonodooodoo-12

Expected singleton Error "for" loop - Odoo server actions


I am using odoo web UI.

I am creating a new button in sale.order form to create RFQ.

The server action to create the RFQ is working well but not for the one2many order_line field.

I am getting the Expected singleton error

raise ValueError("Expected singleton: %s" % record)
    ValueError: <class 'ValueError'>: "Expected singleton: sale.order.line(706, 707)" while evaluating

Here is the code I use to loop through the records:

for rec in record:
  if record.order_line:
    for line in record.order_line:
      if line.id:
        action = {
        "type": "ir.actions.server",
        "id": 601,
        "context": {"active_id": record.order_line.id, "active_model": "sale.order.line"}
        }

The "id":601 action is defined through the UI too: action ID 601

I tried to use this answer: ValueError: Expected singleton: - Odoo v8

But still have the singleton error.

What am I missing in the code for the action to loop through the lines of the one2many field order_line without the singleton error?


Solution

  • IMO issue is in this line "active_id": record.order_line.id

    record.order_line will have a list of records set. You do loop on it which is correct. line will represent a single record set. So you have to use line.

    Try with following code:

    for rec in record:
        for line in rec.order_line:
            action = {
                "type": "ir.actions.server",
                "id": 601,
                "context": {"active_id": line.id, 
                            "active_model": "sale.order.line"}
            }