openerp中的全局变量和上下文
我需要做的是在保存某个模块之前访问该模块中的列的值 例如,如果我们有这样定义的模块:openerp中的全局变量和上下文,openerp,Openerp,我需要做的是在保存某个模块之前访问该模块中的列的值 例如,如果我们有这样定义的模块: _columns = { 'name': fields.char('Department Name', size=64, required=True), 'complete_name': fields.function(_dept_name_get_fnc, type="char", string='Name'), 'parent_id': fields.many2one('hr.dep
_columns = {
'name': fields.char('Department Name', size=64, required=True),
'complete_name': fields.function(_dept_name_get_fnc, type="char", string='Name'),
'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
'note': fields.text('Note'),
}
所有这些列都由文本框、组合框、复选框等控件表示
当我们在创建模式下打开表单视图时,会创建一条新记录,但不会保存,并且在单击(保存)之前不会在数据库中获取id
问题是如何在保存这些字段(控件)之前以及在当前记录采用新id之前访问这些字段(控件)的值
我知道self.browse和self.search,但他们需要的ID不可用,因为记录尚未保存
此外,我们是否可以分配一个可以从模块中的任何类访问的全局变量(web开发术语中的会话变量)?如果要访问此字段,则需要覆盖orm的create方法。例如:
def create(self, cr, uid, vals, context=None):
name = vals.get('name')
complete_name = vals.get('complete_name') # like all others field if one2many field comes than this field access using for loop.
vals.update({'your_field_name': your_value_what_you_want_to_update_before_creating})
#vals.update({'name': 'Your Good Name'})
return super(your_class_name, self).create(cr, uid, vals, context)
def write(self, cr, uid, ids, vals, context=None):
print "\n\n\nCurrent Values =>", vals
name = vals.get('name')
print "\nCurrent Name => ", name
complete_name = vals.get('complete_name') # like all others field if one2many field comes than this field access using for loop.
#do all calculation as you want and store in local variable and that variable pass as a value. for example.
name = 'Your Good Name'
vals.update({'name': name})
print "\n\nUpdated Values => ", vals
return super(your_class_name, self).write(cr, uid, ids, vals, context)
希望这将对您有所帮助。更好地覆盖create方法,并以您想要的方式使用。在你的物体上试试这个,这是一个例子
def create(self, cr, uid, vals, context=None):
if vals.get('name','/')=='/':
vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '/'
return super(sale_order, self).create(cr, uid, vals, context=context)
同样,您也可以从这里自己进行更改,请参见“VAL”和“context”的值,
这将对您有所帮助。也许我说得不够清楚,我需要的是在运行时访问字段并进行一些计算,而不仅仅是在单击“保存”时。我相信重写create方法只会在我单击(保存)并且记录开始它到数据库的旅程的那一刻起作用。create方法只调用一次,并且您希望执行一些计算,而不是重写write方法。和创建方法代码相同的操作也会起作用。我用write方法更新了我的答案。谢谢你的回答,但是在我们进入数据库之前,没有读取字段值的方法吗?类似于javascript中的(getElementByID)吗?是的,这与javascrip中的getElementByID相同。要读取的内容字段超出了VAL.get可以访问的范围('field_name')。如果您的字段是one2many或many2many,则它将使用loop。我尝试在方法中使用VAL,但此变量仅在create和write方法中定义,否则它必须从另一个方法传递给该方法。你能举个例子吗?