Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

求助通过按钮弹出窗口问题



  • 目前想实现一个功能,点击按钮弹出一个新窗口,教程上的例子,在xml文件的button name那里指定action,不太符合我的要求,我希望做到的是如下:
    py文件:

    <br />def button_event()<br />&nbsp; &nbsp; status=XX&nbsp;  <br />&nbsp; &nbsp; if status=true<br />&nbsp; &nbsp; &nbsp;  XXX<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp; &nbsp;  pop_window()&nbsp; &nbsp; &nbsp; <br />
    



    XML文件

    <br />&lt;record model=&quot;ir.actions.act_window&quot; id=&quot;action_my_action&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;My Action&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;res_model&quot;&gt;my.class&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;view_type&quot;&gt;form&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;view_mode&quot;&gt;form&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;view_id&quot; ref=&quot;view_my_action&quot;/&gt;<br />&lt;/record&gt;<br /><br />&lt;button name=&quot;button_event&quot; string=&quot;Event&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; states=&quot;draft,open&quot; type=&quot;object&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icon=&quot;gtk-media-pause&quot; /&gt;<br />
    



    如上述代码,点击button的时候,触发的是button_event函数,这个函数中有一个判断,满足条件才会弹出一个窗口,否则做其他的事情,而弹出窗口的函数pop_window(),现在遇到了问题,我要弹出的是view_my_action这个view,所用的代码如下:

    <br />data_obj = self.pool.get(&#039;ir.model.data&#039;)<br />data_id = data_obj._get_id(cr, uid, &#039;my.class&#039;, &#039;action_my_action&#039;)<br />
    


    则系统提示:No references to my.class.action_my_action
    请问在pop_window函数中,如何才能与action_my_action或者view_my_action关联使之能够弹出呢?
    非常感谢!


  • 管理员

    data_id = data_obj._get_id(cr, uid, &#039;my.class&#039;, &#039;action_my_action&#039;)
    


    上面的代码中‘my.class'的位置应该是你的模块的名字,而不是model name

    另外,你本意可能是使用 [tt]get_object_reference[/tt]函数,该函数返回的是指定xml_id所对应的资源对象的model(即数据库表)和res_id(表中的记录号)



  • 非常感谢digitalsatori,那行代码改成模块名字后,没有提示错误了,但是那个该死的窗口死活也不出来,请再帮忙看看是什么问题。非常感谢。

    <br />def pop_window(self, cr, uid, ids, context=None):<br />&nbsp;  mod_obj = self.pool.get(&#039;ir.model.data&#039;) <br />&nbsp;  form_res = mod_obj.get_object_reference(cr, uid, &#039;my_module_name&#039;, &#039;action_my_action&#039;) <br />&nbsp;  form_id = form_res and form_res[1] or False<br />&nbsp;  value = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;: _(&#039;Name&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;view_type&#039;: &#039;form&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;view_mode&#039;: &#039;form&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;res_model&#039;: &#039;my.class&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;views&#039;: [(form_id, &#039;form&#039;)],<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;type&#039;: &#039;ir.actions.act_window&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br />&nbsp;  return value<br /><br />def button_event(self, cr, uid, ids, *args):<br />&nbsp;  self.pop_window(cr, uid, ids, None)<br />
    

  • 管理员

    你的form_res取到的是一个action id, 事实上你希望得到的是一个view id, 改成如下:

    form_res = mod_obj.get_object_reference(cr, uid, &#039;my_module_name&#039;, &#039;view_my_action&#039;)
    



    并且在返回字典中添加:

    &#039;target&#039;: &#039;new&#039;
    



  • 非常感谢,但是这样做也还是弹不出来窗口,我把我的思路再梳理一下,麻烦请帮忙看看是否还有其他地方出错了,非常感谢。
    新建一个模块,文件夹名称:my_module_name,其中有my_module_name.py 和 my_module_name_view.xml

    py文件:my_class类中有button_event和pop_window两个函数,其中button_event函数需要调用pop_window。在pop_window函数中,关联了xml定义的view_my_action这个form,希望能弹出这个form的窗口。

    <br />class my_class(osv.osv):<br />&nbsp; &nbsp; _name=&quot;my.class&quot;<br />&nbsp; &nbsp; _columns={<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#039;name&#039;: fields.char(&#039;名称&#039;, size=128, required=True), <br />&nbsp; &nbsp; }<br /><br />&nbsp;  def pop_window(self, cr, uid, ids, context=None):<br />&nbsp; &nbsp; &nbsp; mod_obj = self.pool.get(&#039;ir.model.data&#039;) <br />&nbsp; &nbsp; &nbsp; form_res = mod_obj.get_object_reference(cr, uid, &#039;my_module_name&#039;, &#039;view_my_action&#039;) <br />&nbsp; &nbsp; &nbsp; form_id = form_res and form_res[1] or False<br />&nbsp; &nbsp; &nbsp; value = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039;: _(&#039;Name&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;view_type&#039;: &#039;form&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;view_mode&#039;: &#039;form&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;res_model&#039;: &#039;my.class&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;views&#039;: [(form_id, &#039;form&#039;)],<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;type&#039;: &#039;ir.actions.act_window&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;target&#039;: &#039;new&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br />&nbsp; &nbsp; &nbsp; return value<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; def button_event(self, cr, uid, ids, *args):<br />&nbsp; &nbsp; &nbsp; &nbsp; self.pop_window(cr, uid, ids, None)<br />my_class()<br />
    



    XML文件:有my_class_form_view和view_my_action这两个form,其中my_class_form_view是主form,上面有一个button,关联函数button_event,希望通过这个按钮,能弹出view_my_action这个form。

    <br />&lt;record id=&quot;view_my_action&quot; model=&quot;ir.ui.view&quot;&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;my.class.form1&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;my.class&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;type&quot;&gt;form&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;form string=&quot;Button Action&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/form&gt;<br />&nbsp; &nbsp; &lt;/field&gt;<br />&lt;/record&gt;<br /><br />&lt;record id=&quot;my_class_form_view&quot; model=&quot;ir.ui.view&quot;&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;my.class.form&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;my.class&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;type&quot;&gt;form&lt;/field&gt;<br />&nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;form string=&quot;弹窗&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;button name=&quot;button_event&quot; string=&quot;pop&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; states=&quot;open,draft,pending&quot; type=&quot;object&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icon=&quot;gtk-go-up&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/form&gt;<br />&nbsp;  &lt;/field&gt;<br />&lt;/record&gt;<br />
    

  • 管理员

    定义了my.class Model定义_columns了吗, 另外 'target': 'new' 加 了吗?
    有什么错误信息?



  • 定义了columens了,target:new也增加了,没有错误信息。程序里面设置的是点了这个按钮,弹出窗口,然后state,也就是阶段变成变成下一阶段。现在阶段变了,但是窗口没有弹出来。


  • 管理员

    这样交流有点累,每次只挤一点信息给我。不知道你在上面贴的那个

     &#039;target&#039;: new
    

    , 和你实际的代码是否一致,应该是[quote]'target':'new'[/quote]



  • 非常抱歉,我已经把4楼的代码重新核对过了,现在应该没有问题了。
    另外,我又做了测试,发现如果在xml的button那里直接调用pop_window是可以弹出窗口的,调用button_event却不行,那么应该说明是button_event里面的调用代码:self.pop_window(cr, uid, ids, None) 这句出了问题。
    请问是否这样的调用是不允许的,或者语法上有其他错误吗?
    非常感谢!


  • 管理员

    试试这样:

    def button_event(self, cr, uid, ids, *args):<br />&nbsp; &nbsp; return self.pop_window(cr, uid, ids, None)<br />
    


  • 非常感谢,这样做可以了。