Caludio
Caludio

Reputation: 135

Call @api on header of function

So, I'm working on this object where he inherits from another object ODOO v8

class a(models.Model):
_inherit = 'my.inherit.object'

@api.multi
def _default_group(self):
    domain = {'group_id': [('id', 'in', False)]}
    user_c = self.env['timetable'].search([
        # ('company_id', '=', self.env.user.company_id.id),
        ('year_id', '=', self.year_id),
        ('user_id', '=', self.user_id.id), ])
    if not teacher:
        raise exceptions.ValidationError(_("you haven't access, sorry so contact your administrator"))

    groups = []
    for line_groups in user_c:
        if line_groups.cd_id == self.cd_id:
            groups.append(line_groups.group_id.id)
        domain['group_id'] = [('id', 'in', groups)]


    return {'domain': domain}

so when I try to start this test code he shows me this error

Expected singleton: my.inherit.object ('lang', 'tz', 'params', 'uid', 'active_model')

What can I do to fix that, all fields are working and also the treatment is working fine, but it stops and shows me an error.

Upvotes: 1

Views: 59

Answers (3)

CZoellner
CZoellner

Reputation: 14776

If your method _default_group is used as default method - getting default values for a field - self is an empty recordset. But you're using self as singleton for example in ('year_id', '=', self.year_id). That won't work. To get values like year or whatever in default value methods, you will have to find a way around self.

Upvotes: 0

Nitin Kantak
Nitin Kantak

Reputation: 168

Try this:

for line_groups in user_c:
    for rec in self:
        if line_groups.cd_id == rec.cd_id:
            groups.append(line_groups.group_id.id)
    domain['group_id'] = [('id', 'in', groups)]

Upvotes: 0

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

self.ensure_one is made to ensure that only one record is being passed on. It checks that the current record is a singleton (so just one record, not multiple records).

If self would contain multiple records the system would throw up an error.

You may try with following code:

@api.multi
def _default_group(self):
    self.ensure_one()
    # your logic start from here

Upvotes: 1

Related Questions