Python odoo 上下文字段。弹出窗口的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34926074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
odoo context field. default value for popup
提问by Danila Ganchar
I work with Odoo(v9). I have custom: form(for model 1), action and popup with form(for model 2). Here example main form:
我使用 Odoo(v9)。我有自定义:表单(对于模型 1)、动作和带有表单的弹出窗口(对于模型 2)。这里示例主窗体:
<record id="my_id_form" model="ir.ui.view">
<field name="name">my_name_form</field>
<field name="model">my_model_one</field>
<field name="arch" type="xml">
<form string="Name">
<sheet>
<group>
<field name="partner_id"/>
<!-- button which open popup with my_model_two -->
<button string="Open popup"
name="%(my_module.action_open_popup)d"
type="action"
class="btn-link"/>
</group>
</sheet>
</form>
</field>
</record>
Window action for button Open popup:
按钮打开弹出窗口的窗口操作:
<record id="action_open_popup" model="ir.actions.act_window">
<field name="name">action name</field>
<field name="res_model">my_model_two</field>
<field name="view_id" ref="model_two_form_popup"/>
<!--
How I can send partner_id from main form to popup?
I tried different ways in context field, but all in vain
<field name="context">{'default_partner_id': ?????,}</field>
-->
<field name="target">new</field>
</record>
Example my_model_one
示例my_model_one
class MyModelOne(models.Model):
_name = 'my_model_one'
partner_id = fields.Many2one('res.partner', string='Partner')
Popup form:
弹出窗体:
<record id="model_two_form_popup" model="ir.ui.view">
<field name="name">Popup name</field>
<field name="model">my_model_two</field>
<field name="arch" type="xml">
<form string="Popup text">
<sheet>
<group>
<field name="partner_id" invisible="1"/>
<group>
</sheet>
</form>
</field>
</record>
My question is: How I can send value from field in main formto popup form?(partner_id)
我的问题是:如何将主表单中的字段值发送到弹出表单?(partner_id)
I saw how in the code used active_id, string or integer values. But I have not found how to send fields values or how to register method for custom logic. Can someone provide a small example? Thanks in advance.
我在代码中看到了如何使用active_id、字符串或整数值。但我还没有找到如何发送字段值或如何为自定义逻辑注册方法。有人可以提供一个小例子吗?提前致谢。
采纳答案by Danila Ganchar
I found the solution. In element button need to add contextlike this:
我找到了解决方案。在元素按钮中需要添加这样的上下文:
<button string="Open popup"
name="%(my_module.action_open_popup)d"
type="action"
class="btn-link"
<!-- name_of_parameter: name_of_field -->
context="{'partner_id': partner_id}"/>
After this we need set default value to popup:
在此之后,我们需要设置默认值来弹出:
<record id="action_open_popup" model="ir.actions.act_window">
<field name="name">action name</field>
<field name="res_model">my_model_two</field>
<field name="view_id" ref="model_two_form_popup"/>
<!-- set default value to field from context parameter by name -->
<field name="context">{'default_partner_id': context.get('partner_id', False),}</field>
<field name="target">new</field>
</record>
回答by Charif DZ
you can do better the context could be dynamic when you use a python method to open the popup see example in the odoo addons :
当您使用 python 方法打开弹出窗口时,您可以做得更好,上下文可以是动态的,请参见 odoo 插件中的示例:
@api.multi
def open_popup(self)
#the best thing you can calculate the default values
# however you like then pass them to the context
return {
'name': 'Import Module',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'res_model': 'model.name',
'type': 'ir.actions.act_window',
'context': {'default_partner_id':value,'default_other_field':othervalues},
}
回答by Alexander Kopylov
In view xml:
在视图 xml 中:
<field
name="item_ids"
nolabel="1"
domain="['apl_id','=',active_id]"
context="{'res_id':active_id}">
and in model.py (items):
在model.py(项目)中:
_defaults = {
"res_id": lambda self,cr,uid,c:c.get('res_id',False)
}