问题描述
在 product.template 模型中,我添加了新的智能按钮,该按钮将 ir.actions.act_window 操作返回到模型 stock.quant ,当单击同一按钮时,如果有警告,我还想返回警告产品基于条件: 这是警告代码:
if not self or not self.env.user.has_group('sale.group_warning_sale'):
return
if self.sale_line_warn != 'no-message':
if self.sale_line_warn_msg:
message = self.sale_line_warn_msg
warning = {
'title': ("Warning for %s") % self.name,'message': message
}
if self.sale_line_warn == 'block':
self = False
return {'warning': warning}
这里是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_product_inherit" model="ir.ui.view">
<field name="name">view_product_inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<div name="button_Box" position="inside">
<button class="oe_stat_button" name="my_action"
type="object" icon="fa-shopping-cart" string="Stock">
</button>
</div>
</field>
</record>
</odoo>
这是我的功能:
def my_action(self):
return {
'name': _('Stock'),'view_type': 'form','view_mode': 'tree','res_model': 'stock.quant','context': {'default_name': 'My warning message'},'type': 'ir.actions.act_window',}
*有什么帮助吗?有可能做到吗? 谢谢
解决方法
我能想到的简单解决方案是向 boolean
模型添加一个 datetime
字段和一个 product.template
字段。在操作中,如果有任何警告,请检查 boolean
字段是否为 True
并且如果从 datetime
字段传递的时间小于例如 30 秒,则不显示警告并返回您的 ir.actions.act_window
操作。但是,如果 boolean
字段是 False
,则通过引发 UserWarning
或 ValidationError
来显示警告,并在警告消息的末尾询问用户“如果您确定并想继续,请再次单击该按钮”。并且不要忘记在显示返回警告时将 boolean
字段设置为 True
并将 datetime
字段设置为当前时间。返回操作时,还将 boolean
字段设置为 False
。
这样用户只会收到一次警告,如果他想继续,他可以再次点击按钮。
,在 my_action 方法的顶部添加一个条件(在返回操作之前),如下所示:
if condition:
raise ValidationError("Error message")
不要忘记在 .py 文件的顶部导入 ValidationError。
from odoo.exceptions import ValidationError