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

Hello odoo experts, 

I just want to generate exam results according to the selected subjects. And my code is also ready for this. But when I execute this unfortunately the system response the error "ValueError: expected singleton: exam.subject(101, 102)".

I don't know what I'm missing here. Except your assist.

Thanks in Advance.


Here is my code:

# Method to Generate Result

def generate_result(self):
"""Method to generate result"""
result_obj = self.env["exam.result"]
student_obj = self.env["student.student"]
result_list = []
for rec in self:
for exam_schedule in rec.exam_schedule_ids:
string = [
("standard_id", "=", exam_schedule.standard_id.id),
("year", "=", rec.academic_year.id),
("state", "=", "done"),
("school_id", "=", exam_schedule.standard_id.school_id.id),
]
students_rec = student_obj.search(string)
for student in students_rec:
string = [
("standard_id", "=", student.standard_id.id),
("student_id", "=", student.id),
("s_exam_ids", "=", rec.id),
]
exam_result_rec = result_obj.search(string)
if exam_result_rec:
[result_list.append(res.id) for res in exam_result_rec]
else:
rs_dict = {
"s_exam_ids": rec.id,
"student_id": student.id,
"standard_id": student.standard_id.id,
"roll_no_id": student.roll_no,
"grade_system": rec.grade_system.id,
}
exam_line = []
timetable = exam_schedule.sudo().timetable_id
for line in timetable.sudo().timetable_ids:
min_mrks = line.subject_id.minimum_marks
max_mrks = line.subject_id.maximum_marks
sub_vals = {
"subject_id": line.subject_id.id,
"minimum_marks": min_mrks,
"maximum_marks": max_mrks,
}
exam_line.append((0, 0, sub_vals))
rs_dict.update({"result_ids": exam_line})
result_rec = result_obj.create(rs_dict)
result_list.append(result_rec.id)
return {
"name": _("Result Info"),
"view_mode": "tree,form",
"res_model": "exam.result",
"type": "ir.actions.act_window",
"string": [("id", "in", result_list)],
}

#Model 

class ExamResult(models.Model):

    """Defining Exam Result."""
    _name = "exam.result"
    _inherit = ["mail.thread", "resource.mixin"]
    _rec_name = "roll_no_id"
    _description = "exam result Information"
    
  standard_id = fields.Many2one(
    "school.standard", "Standard", help="Select Standard")
  result_ids = fields.One2many(
    "exam.subject", "exam_id", "Exam Subjects", help="Select exam subjects",
  )
  #.... other attributes

  @api.model
def create(self, vals):
"""Inherited the create method to assign the roll no and std"""
if vals.get("student_id"):

student_rec = self.env["student.student"].browse(
vals.get("student_id")
)
vals.update(
{
"roll_no_id": student_rec.roll_no,
"standard_id": student_rec.standard_id.id,
}
)
return super(ExamResult, self).create(vals)
  

Avatar
Discard

Can u please add the full error log

Author

@Deerom Technologies Pvt.Ltd, Thanks for your response. This is the full error log-

Odoo Server Error

Traceback (most recent call last):

File "F:\odoo-14.0\odoo\odoo\addons\base\models\ir_http.py", line 237, in _dispatch

result = request.dispatch()

File "F:\odoo-14.0\odoo\odoo\http.py", line 682, in dispatch

result = self._call_function(**self.params)

File "F:\odoo-14.0\odoo\odoo\http.py", line 358, in _call_function

return checked_call(self.db, *args, **kwargs)

File "F:\odoo-14.0\odoo\odoo\service\model.py", line 94, in wrapper

return f(dbname, *args, **kwargs)

File "F:\odoo-14.0\odoo\odoo\http.py", line 346, in checked_call

result = self.endpoint(*a, **kw)

File "F:\odoo-14.0\odoo\odoo\http.py", line 911, in __call__

return self.method(*args, **kw)

File "F:\odoo-14.0\odoo\odoo\http.py", line 530, in response_wrap

response = f(*args, **kw)

File "f:\odoo-14.0\odoo\addons\web\controllers\main.py", line 1363, in call_button

action = self._call_kw(model, method, args, kwargs)

File "f:\odoo-14.0\odoo\addons\web\controllers\main.py", line 1351, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "F:\odoo-14.0\odoo\odoo\api.py", line 396, in call_kw

result = _call_kw_multi(method, model, args, kwargs)

File "F:\odoo-14.0\odoo\odoo\api.py", line 383, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "f:\odoo-14.0\odoo\custom-addons\odooeduerp-14.0\exam\models\exam.py", line 414, in generate_result

result_rec = result_obj.create(rs_dict)

File "<decorator-gen-202>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 323, in _model_create_single

return create(self, arg)

File "f:\odoo-14.0\odoo\custom-addons\odooeduerp-14.0\exam\models\exam.py", line 686, in create

return super(ExamResult, self).create(vals)

File "<decorator-gen-130>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 344, in _model_create_multi

return create(self, [arg])

File "f:\odoo-14.0\odoo\addons\mail\models\mail_thread.py", line 262, in create

threads = super(MailThread, self).create(vals_list)

File "<decorator-gen-117>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 326, in _model_create_single

return self.browse().concat(*(create(self, vals) for vals in arg))

File "F:\odoo-14.0\odoo\odoo\api.py", line 326, in <genexpr>

return self.browse().concat(*(create(self, vals) for vals in arg))

File "f:\odoo-14.0\odoo\addons\resource\models\resource_mixin.py", line 46, in create

return super(ResourceMixin, self).create(values)

File "<decorator-gen-65>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 344, in _model_create_multi

return create(self, [arg])

File "F:\odoo-14.0\odoo\odoo\addons\base\models\ir_fields.py", line 508, in create

recs = super().create(vals_list)

File "<decorator-gen-13>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 345, in _model_create_multi

return create(self, arg)

File "F:\odoo-14.0\odoo\odoo\models.py", line 3827, in create

records = self._create(data_list)

File "F:\odoo-14.0\odoo\odoo\models.py", line 3987, in _create

for other, data in zip(others, data_list)

File "F:\odoo-14.0\odoo\odoo\fields.py", line 2970, in create

self.write_batch(record_values, True)

File "F:\odoo-14.0\odoo\odoo\fields.py", line 2996, in write_batch

return self.write_real(records_commands_list, create)

File "F:\odoo-14.0\odoo\odoo\fields.py", line 3168, in write_real

flush()

File "F:\odoo-14.0\odoo\odoo\fields.py", line 3132, in flush

comodel.create(to_create)

File "<decorator-gen-65>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 345, in _model_create_multi

return create(self, arg)

File "F:\odoo-14.0\odoo\odoo\addons\base\models\ir_fields.py", line 508, in create

recs = super().create(vals_list)

File "<decorator-gen-13>", line 2, in create

File "F:\odoo-14.0\odoo\odoo\api.py", line 345, in _model_create_multi

return create(self, arg)

File "F:\odoo-14.0\odoo\odoo\models.py", line 3827, in create

records = self._create(data_list)

File "F:\odoo-14.0\odoo\odoo\models.py", line 4000, in _create

records._validate_fields(name for data in data_list for name in data['stored'])

File "F:\odoo-14.0\odoo\odoo\models.py", line 1249, in _validate_fields

check(self)

File "f:\odoo-14.0\odoo\custom-addons\odooeduerp-14.0\exam\models\exam.py", line 824, in _validate_marks

maximum_marks = self.maximum_marks if self.maximum_marks else self.subject_id.maximum_marks

File "F:\odoo-14.0\odoo\odoo\fields.py", line 926, in __get__

record.ensure_one()

File "F:\odoo-14.0\odoo\odoo\models.py", line 4950, in ensure_one

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

Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "F:\odoo-14.0\odoo\odoo\http.py", line 638, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "F:\odoo-14.0\odoo\odoo\http.py", line 314, in _handle_exception

raise exception.with_traceback(None) from new_cause

ValueError: Expected singleton: exam.subject(117, 118)

can u please check the method that you defined under exam\models\exam.py", line 824, in _validate_mark

can u please check this mehtod "_validate_mark".Not sure but think the error is coming from this method.

In this method it Expected singleton but self contains multiple recordset.

Author

@Deerom Technologies Pvt.Ltd, Thanks again. But When I select a single subject it works perfectly. The error is occurred in case of multiple subject is selected.

Hi,

can u please add the method code here.

Author

@Deerom Technologies Pvt.Ltd, Thank you so much. You are saved my time. I wasted 2 days for this. Now the problem is solved just using a loop on the "_validate_marks" method.

Best Answer

if self.test_field: #face ValueError Expected singleton because self contains multiple recordset.

Need to change with following:

for record in self:

    if record.test_field:

use decorators @api.multi with self.ensure_one() or use api.one and make changes accordingly

Avatar
Discard
Related Posts Replies Views Activity
4
Mar 24
299
2
Sep 22
1448
4
Feb 19
2671
3
Jun 22
4334
4
Dec 19
4305