odoo context上下文通俗理解

在Odoo中,Context用于在不同作业界面间传递信息,例如在课程界面选课时,不仅传递课程数据,还需传递操作员信息。通过with_context方法,下游界面能获取上游界面的特定信息,如用户名,以实现更复杂的业务逻辑。
摘要由CSDN通过智能技术生成

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

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

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

此时就需要用到context了。

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from odoo import models, fields, api

  4. from odoo.exceptions import UserError, ValidationError

  5. from odoo.modules.module import get_module_resource

  6. from odoo import tools, _

  7. import random

  8. import time

  9.  
  10.  
  11. class course(models.Model):

  12. _name = 'xksystem.course'

  13. _description = 'xksystem.course'

  14.  
  15. name = fields.Char(string=u'课程名')

  16. code = fields.Char(string=u'课程编号')

  17. studentlimit = fields.Integer(string=u'学生上限')

  18. havestudent = fields.Integer(string=u'已选课学生数', compute='_compute_have_student')

  19. teachers = fields.Many2many('xksystem.teacher', 'xksystem_course_xksystem_teacher_rel',

  20. 'course_id', 'teacher_id', string='授课教师')

  21. course_score = fields.Float(string='课程学分')

  22. remarks = fields.Html()

  23.  
  24. @api.constrains('course_score')

  25. def check_course_score(self):

  26. for record in self:

  27. if record.course_score > 2:

  28. raise ValidationError("The Score is too High: %s" % record.course_score)

  29.  
  30. #--重写name_get方法,同时返回课程名称和代码,这样别人在调用的时候就会明确的知道课程名称和课程代码

  31. @api.multi

  32. def name_get(self):

  33. result = []

  34.  
  35. for record in self:

  36. result.append((record.id, "%s(%s)" % (record.name, record.code)))

  37.  
  38. return result

  39.  
  40. @api.multi

  41. def _compute_have_student(self):

  42. for recode in self:

  43. l_sql = "select course_id,count(*) from xksystem_studentcourseline " \

  44. "where course_id = %s group by course_id" % (recode.id)

  45. self.env.cr.execute(l_sql)

  46. dicts = self.env.cr.dictfetchall()

  47. #print(dicts)

  48. if len(dicts) > 0:

  49. recode.havestudent = dicts[0]['count']

  50. else:

  51. recode.havestudent = 0

  52.  
  53. @api.multi

  54. def xk_btn(self):

  55. # --点击选课按钮,然后创建一笔学生课程资料

  56. res = self.env['res.users'].search([('id', '=', self.env.uid)]) # 获取当前用户的ID

  57. print(res.login)

  58. code = res.login # 获取当前用户的学号

  59. res = self.env['xksystem.student'].search([('code', '=', code)]) # 以学号获取当前学生头表信息

  60. if res.id:

  61. # 检索是否已经选过此门课程

  62. res_course = self.env['xksystem.studentcourseline'].search(['&', ('student_id', '=', res.id),

  63. ('coursecode', '=', self.id)])

  64. if res_course:

  65. print('此门课程已被选过了,不能重复选择!')

  66. raise UserError(('你已经选过了这门课,不能重复选择!'))

  67. else:

  68. # 防止大量并发选课,每位学生随机停止0-1秒

  69. sleep_time = random.random()

  70. print(sleep_time)

  71. time.sleep(sleep_time)

  72.  
  73. # 检索课程是否已经被选光

  74. l_sql = "select course_id,count(*) from xksystem_studentcourseline " \

  75. "where course_id = %s group by course_id" % (self.id)

  76. self.env.cr.execute(l_sql)

  77. dicts = self.env.cr.dictfetchall()

  78. if len(dicts)==0:

  79. havastudent_count = 0

  80. else:

  81. havastudent_count = dicts[0]['count']

  82.  
  83. if havastudent_count >= self.studentlimit:

  84. raise UserError(('选课学生人数已经超过上限,请选择其他课程!'))

  85.  
  86. # 合规,系统进行选课

  87. #在课程类中组学生课程信息,同时将当前操作用户传递过去,然后在student类中判断当前用户的合法性

  88. context = {'用户名':code}

  89. print('context:',context)

  90. vals = {'linenumber': self.env['ir.sequence'].next_by_code('seq.test'), 'student_id': res.id,

  91. 'course_id': self.id, 'coursecode': self.code,}

  92. self.with_context(context).env['xksystem.studentcourseline'].sudo().create(vals)

  93. else:

  94. raise UserError(('此账户不是学生账户,不能选课!'))

  95. return True

 
  1. class StudentCourseLine(models.Model):

  2.  
  3. @api.model_create_multi

  4. def create(self, vals_list):

  5. limit_student = self.env.context.get('用户名',False) #从上下文中获取对应的学生,限制其选课

  6. if limit_student == 'S081001':

  7. raise UserError(('此学生被限制选课,请联系学院管理员!'))

  8. for rec in self:

  9. print('----context test-----')

  10. print(rec.context_get())

  11. print('----context test-----')

  12. return super(StudentCourseLine, self).create(vals_list)

 

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

键值,进而逻辑判断

转自https://www.cnblogs.com/smarttony/p/11958526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值