Search code examples
odoovalueerrorraiseerror

Odoo: ValueError("Expected singleton: %s" % self)


I'm modifying Odoo OpenEduCat exam module to fit the need of my institution. For that, I have tailored the code as shown below. However,when I click on generate button, odoo raises expected singleton error. Generating button Error details

--Python code--

from openerp import models, fields, api

class OpResultTemplate(models.Model):
    _name = 'op.result.template'
    _description = 'Result Template'
    _rec_name = 'name'

    exam_session_id = fields.Many2one(
    'op.exam.session', 'Exam Session', related='line_ids.exam_session_id', required=False)
    name = fields.Char("Name", size=254, required=True)
    result_date = fields.Date(
    'Result Date', required=True, default=fields.Date.today())
    line_ids = fields.One2many(
    'op.result.template.line', 'result_id', 'Session Lines')
####this is for semester
    inter1_ids = fields.One2many(
    'op.internal1', 'result_id', 'Internal 01')
    inter2_ids = fields.One2many(
    'op.internal2', 'result_id', 'Internal 02')
    model_ids = fields.One2many(
    'op.model', 'result_id', 'Model')
    final_ids = fields.One2many(
    'op.final', 'result_id', 'Semester')
    state = fields.Selection(
    [('normal', 'Normal'), ('semester', 'Semester')],
    string='State', required=True, default='normal')
# pass_status_ids = fields.Many2many('op.pass.status', string='Pass Status')

    @api.one
    def generate_result(self):
        data = self.read(['state'])[0]
        if data['state'] == 'normal' :
        ####Write information in to Marksheet Register the place where result generate to.
            marksheet_reg_id = self.env['op.marksheet.register'].create({
            'name': 'Mark Sheet for %s' % self.line_ids.exam_session_id.name,
            'exam_session_id': self.line_ids.exam_session_id.id,
            'generated_date': fields.Date.today(),
            'generated_by': self.env.uid,
            'status': 'draft',
            'course_id': self.line_ids.exam_session_id.course_id.name,
            'batch_id': self.line_ids.exam_session_id.batch_id.name,
            'exam_type': self.line_ids.exam_session_id.exam_type.name,
            'semester_id': self.line_ids.exam_session_id.semester_id.name,
              })
            student_list = []####Define array to store
            for exam_session in self.line_ids:####line_ids is table that located in Result generator which allow to choose exam session
                total_exam = 0.0#global var
                for exam in exam_session.exam_session_id:####exam_session.exam_lines is the table that list the exam or subject located in Result generator->Exam session
                    total_exam += exam.exam_ids.total_marks

                    for attd in exam.exam_ids.attendees_line:####exam.exam_id.attendees_line location that contant student name and mark in each subject
                        result_dict = {####this loop is to write information to result line
                        'exam_id': exam.exam_ids.id,
                        'exam_tmpl_id': exam.exam_ids.id,
                        'marks': attd.marks,####IMPORTANCE mark that student get in each subject THIS IS WHERE TO APPLY PERCENTAGES
                        'status': attd.marks >= exam.exam_ids.min_marks and####IMPORTANCE take the mark and decide pass or fail base on passing mark in each subject
                        'pass' or 'fail',
                        'per': (100 * attd.marks) / exam.exam_ids.total_marks,####NOT IMPORTANCE this can be delete, this take the mark student get and find the percentage of the subject student get in each subject
                        'student_id': attd.student_id.id,####student name
                        'total_marks': exam.exam_ids.total_marks,####the total mark of each subject that have been enter when created subject for exam
                    }

--Error details--

Odoo Server Error

Traceback (most recent call last):

File "/home/v4d/odoo/openerp/http.py", line 650, in _handle_exception return super(JsonRequest, self)._handle_exception(exception)

File "/home/v4d/odoo/openerp/http.py", line 687, in dispatch result = self._call_function(**self.params)

File "/home/v4d/odoo/openerp/http.py", line 323, in _call_function return checked_call(self.db, *args, **kwargs)

File "/home/v4d/odoo/openerp/service/model.py", line 118, in wrapper return f(dbname, *args, **kwargs)

File "/home/v4d/odoo/openerp/http.py", line 316, in checked_call result = self.endpoint(*a, **kw)

File "/home/v4d/odoo/openerp/http.py", line 966, in call return self.method(*args, **kw)

File "/home/v4d/odoo/openerp/http.py", line 516, in response_wrap response = f(*args, **kw)

File "/home/v4d/odoo/addons/web/controllers/main.py", line 899, in call_button action = self._call_kw(model, method, args, {})

File "/home/v4d/odoo/addons/web/controllers/main.py", line 887, in _call_kw return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)

File "/home/v4d/odoo/openerp/api.py", line 250, in wrapper return old_api(self, *args, **kwargs)

File "/home/v4d/odoo/openerp/api.py", line 421, in old_api result = new_api(recs, *args, **kwargs)

File "/home/v4d/odoo/openerp/api.py", line 425, in new_api result = [method(rec, *args, **kwargs) for rec in self]

File "/home/v4d/odoo/addons/openeducat_exam/models/result_template.py", line 71, in generate_result total_exam += exam.exam_ids.total_marks

File "/home/v4d/odoo/openerp/fields.py", line 821, in get record.ensure_one()

File "/home/v4d/odoo/openerp/models.py", line 5432, in ensure_one raise ValueError("Expected singleton: %s" % self)

ValueError: Expected singleton: op.exam(44, 45, 46)

I have tried other solutions that could be found on the Internet, but it didn't seem to work. Please kindly help me to deal with this.Thank in advance.


Solution

  • Here is the issue in your code,

    ####IMPORTANCE take the mark and decide pass or fail base on passing mark in each subject
    'status': attd.marks >= exam.exam_ids.min_marks and 'pass' or 'fail',
    

    exam.exam_ids it will return list of browsable objects (recordset list) and you are trying to access min_marks properties, so here it gets confused min_marks property from which object. So it raise an error.

    So either you need to specify single object by specifying exam.exam_ids[0] (only single object will return) or you need to search proper records from the one2many model and then you can access to the min_marks field.

    Properties are separately created for all objects (OOP rule). Static properties will be accessible via class.