2

I have a button in a form that when clicked is supposed to update the current model mrl with data from the spray.action model. Then I do further processing but it raises the error

ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: spray.action(1, 2)
@api.multi
def mrlCreateSprayRecords(self):
    spray_ids = []
    vals = []
    spray_obj = self.env['spray.action'].search([])
    print("spray_obj  \n\n\n\t %s ", spray_obj)
    for obj in spray_obj:
        print("Spray Action Objects  \n\n %s \n\t ", obj)
        vals = {
            'ref': obj.ref,
            'farm': obj.farm.farm,
            'block': obj.block.block,
            'valves': obj.valves.valve,
        }
        print("Spray Action Data Browse , \n\n\t %s ", vals)
        res = super(Mrl, self).create(vals)
        res.update(vals)
        print("object in mrlCreateSprayRecords \n\n\t  %s", res)
    return {
        'name': 'Update Mrl Operations',
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'mrl',
        'views': [(spray_obj.id, 'form')],
        'view_id': spray_obj.id,
        # 'target': 'new',
        'res_id': self.id,
        'context': self.env.context,

    }
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
danielmwai
  • 305
  • 1
  • 6
  • 24

2 Answers2

4

I think you are getting the error in the row 'view_id': spray_obj.id, you must write the view id there. The spray_obj recordset has many record, so you cannot use it like that (spray_obj.id). You can also remove the view_id parameter in order to user the default view.

@api.multi
def mrlCreateSprayRecords(self):
    self.ensure_one()

    spray_obj = self.env['spray.action'].search([])     # recordset of all records of the model????
    for obj in spray_obj:
        vals = {
            'ref': obj.ref,
            'farm': obj.farm.farm,
            'block': obj.block.block,
            'valves': obj.valves.valve,
        }
        self.create(vals)

    view_id = self.env.ref('module.xml_view_id').id         

    return {
        'name': 'Update Mrl Operations',
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'mrl',
        'view_id': view_id,
        'res_id': self.id,
        'context': self.env.context,
    }

I have added self.ensure_one() because res_id has to be only one id as well.

I have remove the res.update(vals) line because it does not make any sense to me haha

More things

Instead of print you should use the logger:

import logging
_logger = logging.getLogger(__name__)

_logger.info('Hello')

Instead the line res = super(Mrl, self).create(vals) I think you should use this one

res = self.create(vals)     # if you are in the mrl model
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
2

In short you get this kind of error when you acces a field direclty of a recordSet that contains more than one record.

You performed a search on the spray.action that search returnd 2 record (1, 2)

So when you do spray_obj.id odoo will be confused what id he should return 1 or 2. And here odoo throw this error.

So dont access a field a search result, or a x2many fiels they may have more than one record inside.

@ChesuCR has improved your code and corrected it

Charif DZ
  • 14,415
  • 3
  • 21
  • 40