act_window中的openerp上下文 [英] openerp context in act_window

查看:81
本文介绍了act_window中的openerp上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenERP 6.1中,此act_window:

In OpenERP 6.1 this act_window:

<act_window
     domain="[('id', '=', student)]"
     id="act_schedule_student"
     name="Student"
     res_model="school.student"
     src_model="school.schedule"/>

时间表表单中创建一个学生按钮,该按钮将打开仅显示适当学生的学生树状视图.

creates a Student button in the Schedule form which opens the student tree view showing only the appropriate student.

我的目标是直接打开学生的相应表单视图,而不是打开具有正确过滤条件的学生的树形视图.我尝试添加view_mode="form,tree",但是它打开了一种新的表单,而不是我想要的表单.我猜这可以通过将context添加到act_window来实现吗?也许是record_id,但是我用active_id尝试了一下,但没有用.

My goal is to directly open the corresponding form view of the student instead of a tree view with the right filtered student. I tried adding a view_mode="form,tree" but it opens a new form instead of the one I want. I'm guessing this can possibly be achieved by adding context to the act_window? Maybe a record_id, but I tried that with active_id and it didn't work.

推荐答案

让OpenERP操作直接打开给定记录的表单视图的一种神奇的方法(可能是未公开的方法)是在上设置一个额外的res_id属性动作.

The magical (and probably undocumented) way to have an OpenERP action directly open the form view of a given record, is to set an extra res_id attribute on the action.

不幸的是,在OpenERP 6.1 [1] 中,res_id属性不是act_window数据模型的一部分,因此无法在XML声明中直接设置它.

Unfortunately in OpenERP 6.1[1] the res_id attribute is not part of the act_window data model, so it is not possible to directly set it in an XML declaration.

大多数官方插件都使用<button type="object" ... />绑定到Python方法,该方法在返回的操作中设置res_id属性.在官方模块的源代码中很容易找到此示例,并且您可以在此看到一个相关问题.

Most official addons use a <button type="object" ... /> bound to a Python method that sets the res_id attribute in the returned action. It is quite easy to find examples of this in the source code of official modules, and you can see one in this related question.

快速/未经测试的示例:

您将以school.schedule形式添加它:

<button name="open_student_form" type="object" string="Student"/>

以及school.schedule模型中的以下方法:

And the following method in the school.schedule model:

def open_student_form(self, cr, uid, ids, context=None):
    this = self.browse(cr, uid, ids, context=context)[0]
    return {
        'type': 'ir.actions.act_window',
        'name': 'Student',
        'view_mode': 'form',
        'view_type': 'form',
        'res_model': 'school.student',
        'nodestroy': 'true',
        'res_id': this.student.id, # assuming the many2one is (mis)named 'student'
        'views': [(False, 'form')],
    }

现在,如果您真的想使用侧边栏按钮"(即使用<act_window/>)来执行此操作,则将变得有些棘手,因为您无法将侧边栏按钮直接绑定到Python方法;它必须绑定到数据库中存储的操作. 但是,它仍然是可行的,例如通过ir.actions.server动作,该动作可以绑定到<act_window/>并调用Python方法或执行类似操作. ir.actions.server的窍门是可以将其定义为Python块,该块可以通过将动作字典分配给action变量来返回动态动作定义. 如果要遵循该路径,请在OpenERP插件源代码中搜索ir.actions.server的声明(其中一些声明可能会执行类似的操作)以及返回带有res_id属性的操作的方法.

Now if you really wanted to do this with a "sidebar button" (i.e. with an <act_window/>), it gets a bit trickier because you cannot directly bind a sidebar button to a Python method; it must be bound to an action that is stored in the database. It is still doable though, for example via an ir.actions.server action that can be bound to your <act_window/> and calls your Python method or does something similar. The trick with ir.actions.server is that it can be defined as a Python block that can return a dynamic action definition by assigning an action dictionary to an action variable. If you want to follow that path, search the OpenERP addons source code for declarations of ir.actions.server (some of them might do similar things) and methods returning actions with a res_id attribute.

[1] 从OpenERP 7.0开始,res_id列在数据模型中显式可用,因此您可以直接对其进行设置.

[1] As of OpenERP 7.0 the res_id column is explicitly available in the data model, so you can directly set it.

这篇关于act_window中的openerp上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆