odoo context上下文通俗理解

断了今生、忘了曾经 提交于 2019-12-06 02:44:52

我们经常在程序中遇到这样的场景,在一个作业界面点击某个按钮或者进行某类操作,然后触发到另外一个作业界面的逻辑,此时我们有时不仅仅

需要将特定的数据库字段传过去,还可能将第一个界面的某些信息传递到跳转的界面。举个例子:

学生选课的时候,在课程界面点击选课,然后会将课程的相关信息传递到学生界面,但是我们还需要将课程界面的操作人员(可能不是学生本人)传递过去,

此时就需要用到context了。

# -*- coding: utf-8 -*-from odoo import models, fields, apifrom odoo.exceptions import UserError, ValidationErrorfrom odoo.modules.module import get_module_resourcefrom odoo import tools, _import randomimport timeclass course(models.Model):    _name = 'xksystem.course'    _description = 'xksystem.course'    name = fields.Char(string=u'课程名')    code = fields.Char(string=u'课程编号')    studentlimit = fields.Integer(string=u'学生上限')    havestudent = fields.Integer(string=u'已选课学生数', compute='_compute_have_student')    teachers = fields.Many2many('xksystem.teacher', 'xksystem_course_xksystem_teacher_rel',                                'course_id', 'teacher_id', string='授课教师')    course_score = fields.Float(string='课程学分')    remarks = fields.Html()    @api.constrains('course_score')    def check_course_score(self):        for record in self:            if record.course_score > 2:                raise ValidationError("The Score is too High: %s" % record.course_score)    #--重写name_get方法,同时返回课程名称和代码,这样别人在调用的时候就会明确的知道课程名称和课程代码    @api.multi    def name_get(self):        result = []        for record in self:            result.append((record.id, "%s(%s)" % (record.name, record.code)))        return result    @api.multi    def _compute_have_student(self):        for recode in self:            l_sql = "select course_id,count(*) from xksystem_studentcourseline " \                    "where course_id = %s group by course_id" % (recode.id)            self.env.cr.execute(l_sql)            dicts = self.env.cr.dictfetchall()            #print(dicts)            if len(dicts) > 0:                recode.havestudent = dicts[0]['count']            else:                recode.havestudent = 0    @api.multi    def xk_btn(self):        # --点击选课按钮,然后创建一笔学生课程资料        res = self.env['res.users'].search([('id', '=', self.env.uid)])  # 获取当前用户的ID        print(res.login)        code = res.login  # 获取当前用户的学号        res = self.env['xksystem.student'].search([('code', '=', code)])  # 以学号获取当前学生头表信息        if res.id:            # 检索是否已经选过此门课程            res_course = self.env['xksystem.studentcourseline'].search(['&', ('student_id', '=', res.id),                                                                        ('coursecode', '=', self.id)])            if res_course:                print('此门课程已被选过了,不能重复选择!')                raise UserError(('你已经选过了这门课,不能重复选择!'))            else:                # 防止大量并发选课,每位学生随机停止0-1秒                sleep_time = random.random()                print(sleep_time)                time.sleep(sleep_time)                # 检索课程是否已经被选光                l_sql = "select course_id,count(*) from xksystem_studentcourseline " \                        "where course_id = %s group by course_id" % (self.id)                self.env.cr.execute(l_sql)                dicts = self.env.cr.dictfetchall()                if len(dicts)==0:                    havastudent_count = 0                else:                    havastudent_count = dicts[0]['count']                if havastudent_count >= self.studentlimit:                    raise UserError(('选课学生人数已经超过上限,请选择其他课程!'))                # 合规,系统进行选课                #在课程类中组学生课程信息,同时将当前操作用户传递过去,然后在student类中判断当前用户的合法性                context = {'用户名':code}                print('context:',context)                vals = {'linenumber': self.env['ir.sequence'].next_by_code('seq.test'), 'student_id': res.id,                        'course_id': self.id, 'coursecode': self.code,}                self.with_context(context).env['xksystem.studentcourseline'].sudo().create(vals)        else:            raise UserError(('此账户不是学生账户,不能选课!'))        return True
class StudentCourseLine(models.Model):    @api.model_create_multi
    def create(self, vals_list):
        limit_student = self.env.context.get('用户名',False)    #从上下文中获取对应的学生,限制其选课
        if limit_student == 'S081001':
            raise UserError(('此学生被限制选课,请联系学院管理员!'))
        for rec in self:
            print('----context test-----')
            print(rec.context_get())
            print('----context test-----')
        return super(StudentCourseLine, self).create(vals_list)

 

这个例子就是从一个类跳转到另外一个类中,将第一个类中的信息用with_context形式传递到下游,然后下游的类再用self.env.context.get('用户名',False)获取对应的

键值,进而逻辑判断

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!