我正在修改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(exception)
文件“/home/v4d/odoo/openerp/http.py”,第687行,发送 result = 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中 result = 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中 response = f(* args,** kw)
文件“/home/v4d/odoo/addons/web/controllers/main.py”,第899行,在call_button中 action = self._call_kw(model,method,args,{})
文件“/home/v4d/odoo/addons/web/controllers/main.py”,第887行,在_call_kw中 return getattr(request.registry.get(model),method)(request.cr,request.uid,* args,** kwargs)
文件“/home/v4d/odoo/openerp/api.py”,第250行,在包装器中 返回old_api(self,* args,** kwargs)
文件“/home/v4d/odoo/openerp/api.py”,第421行,在old_api中 result = new_api(recs,* args,** kwargs)
文件“/home/v4d/odoo/openerp/api.py”,第425行,in new_api result = [方法(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行,获取 record.ensure_one()
文件“/home/v4d/odoo/openerp/models.py”,第5432行,在ensure_one中 提高ValueError(“预期单例:%s”%自我)
ValueError:预期的单例:op.exam(44,45,46)
我尝试过可以在互联网上找到的其他解决方案,但它似乎没有用。请帮助我处理这个问题。谢谢。
答案 0 :(得分:1)
以下是您的代码中的问题,
####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 它将返回可浏览对象列表(记录集列表),并且您正在尝试访问min_marks属性,因此在这里它会混淆min_marks属性来自哪个对象。所以它引发了错误。
因此,您需要通过指定 exam.exam_ids [0] (仅返回单个对象)来指定单个对象,或者您需要从one2many模型中搜索正确的记录然后您可以访问min_marks字段。
为所有对象单独创建属性(OOP规则)。静态的 属性可以通过课程访问。