Skip to Content
Menu
This question has been flagged
1 Reply
6677 Views

Hello,

I have a wizard class, which call a function from another class :

class Wizard_confirmation(models.TransientModel):

_name = "affichage2.wizard_confirmation"

@api.multi
def call_vente(self):

self.env["affichage2.vente_packs"].vendre_cap()


My problem is when i run this function, i got this erreur :

ValueError: Expected singleton: affichage2.vente_packs()


Avatar
Discard
Best Answer

Hello Zakaria,

Singleton error is raised when multiple records or empty records set is got and we update or do any operation without taking for loop or iteration.

self(1,2,3,4,5)
if we access directly from self  (multiple records sets)  then it will raise an error.

for example like t_state = self.state then it will raise an error

ValueError: Expected singleton: model.name(1,2,3,4,5) we can resolve it by taking for loop or iteration in the method.

but if we want to write something then it works
e.g: self.write({'state': 'draft'})


E.g to prevent error:

@api.multi

def  vendre_cap(self)

       for rec in self:

             rec.state = 'draft'

New API decorator is used to defining method calling pattern whether methods allow only single object or multiple objects to invoke this method.

@api.one

This decorator loops automatically on Records of RecordSet for you. Self is redefined as current record

Note: Caution: the returned value is put in a list. This is not always supported by the web client, e.g. on button action methods. In that case, you should use @api.multi to decorate your method and probably call self.ensure_one() in the method definition.

@api.multi

Self will be the current RecordSet without iteration. It is the default behavior (multiple browsable objects). Methods which return non-premitive type data(list, dictionary, function) must be decorated with @api.multi

Thanks

Avatar
Discard
Author

Thank you i will try to fix that using your comment