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

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

我正在修改 Odoo OpenEduCat 考试模块以满足我机构的需要。为此,我定制了如下所示的代码。但是,当我单击生成按钮时,odoo 会引发预期的单例错误。 Generating button Error details

--Python代码--

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
                    }

--错误详情--

Odoo 服务器错误

回溯(最近调用最后):

文件“/home/v4d/odoo/openerp/http.py”,第 650 行,在 _handle_exception return super(JsonRequest, self)._handle_exception(异常)

文件“/home/v4d/odoo/openerp/http.py”,第 687 行,正在调度中 结果 = self._call_function(**self.params)

文件“/home/v4d/odoo/openerp/http.py”,第 323 行,在 _call_function return checked_call(self.db, *args, **kwargs)

文件“/home/v4d/odoo/openerp/service/model.py”,第 118 行,在包装器中 return f(dbname, *args, **kwargs)

文件“/home/v4d/odoo/openerp/http.py”,第 316 行,在 checked_call 结果 = self.endpoint(*a, **kw)

文件“/home/v4d/odoo/openerp/http.py”,第 966 行,在 调用 return self.method(*args, **kw)

文件“/home/v4d/odoo/openerp/http.py”,第 516 行,在 response_wrap 响应 = f(*args, **kw)

文件“/home/v4d/odoo/addons/web/controllers/main.py”,第 899 行,在 call_button 动作 = self._call_kw(模型、方法、参数、{})

文件“/home/v4d/odoo/addons/web/controllers/main.py”,第 887 行,在 _call_kw return getattr(request.registry.get(模型), 方法)(request.cr, request.uid, *args, **kwargs)

文件“/home/v4d/odoo/openerp/api.py”,第 250 行,在包装器中 return old_api(self, *args, **kwargs)

文件“/home/v4d/odoo/openerp/api.py”,第 421 行,在 old_api 结果 = new_api(recs, *args, **kwargs)

文件“/home/v4d/odoo/openerp/api.py”,第 425 行,在 new_api result = [method(rec, *args, **kwargs) for rec in self]

文件“/home/v4d/odoo/addons/openeducat_exam/models/result_template.py”,第 71 行,在 generate_result total_exam += exam.exam_ids.total_marks

文件“/home/v4d/odoo/openerp/fields.py”,第 821 行,在 get 中 record.ensure_one()

文件“/home/v4d/odoo/openerp/models.py”,第 5432 行,在 ensure_one 提高 ValueError("Expected singleton: %s" % self)

ValueError:预期单例:op.exam(44, 45, 46)

我已经尝试了其他可以在 Internet 上找到的解决方案,但似乎没有用。请提前帮我处理好this.Thank。

这是您的代码中的问题,

####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 它将 return 可浏览对象列表(记录集列表)并且您正在尝试访问 min_marks 属性,所以在这里它会混淆 min_marks 属性 来自哪个对象。所以它引发了一个错误。

所以要么你需要通过指定exam.exam_ids[0]来指定单个对象(只有单个对象会return)或者你需要搜索适当的来自 one2many 模型的记录,然后您可以访问 min_marks 字段。

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