让 OpenERP 操作直接打开给定记录的表单视图的神奇(可能没有记录)方法是设置一个额外的res_id
动作上的属性。
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
返回的操作中的属性。在官方模块的源代码中很容易找到这方面的示例,您可以看到一个在这个相关问题中 https://stackoverflow.com/questions/10671025/osv-methods-return-statement.
快速/未经测试的示例:
您可以将其添加到您的school.schedule
form:
<button name="open_student_form" type="object" string="Student"/>
以及下面的方法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
属性。
[1] As of OpenERP 7.0 the res_id
column is explicitly available in the data model, so you can directly set it.