最近在做odoo+vue前后端分离项目,因为需求变更,在odoo这边访问前端的时候需要对路径进行拼接,一开始就是使用“ir.actions.act_url”来直接访问,现在url要根据权限变化了,所以就想直接在菜单中调用方法,然后就有了菜单调用“ir.actions.server”action,然后再这个action中调用模型中的方法,直接上代码。
主页菜单:
<menuitem name="商城" id="menu_shop_root"sequence="40" action="action_shop_server"
web_icon="website_sale,static/description/icon.png"/>
action:这里要注意的就是python代码的格式,格式不对会报错(还有一种说法是Id要有_server,才能正常执行,网上看到的,并没有验证)
<record id="action_shop_server" model="ir.actions.server">
<field name="name">商城</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="base.model_shop_url"/>
<field name="state">code</field>
<field name="code">
url = model.action_shop()
action = { "type": "ir.actions.act_url",
"url": url,
"target": "self",
}
</field>
</record>
model中的方法
ass ShopUrl(models.Model):
_name = 'shop.url'
_description = '访问商城路径'
name = fields.Char(
string='商城名称',
)
shop_url = fields.Char(
string='商城路径'
)
def action_shop(self):
# 获取URL
shop_url = self.search([('name', '=', 'ShopUrl')])
print(shop_url.shop_url)
if not shop_url.shop_url:
raise ValidationError('商城路径不存在,请联系管理员处理!')
# 判断当前登陆人的菜单权限
group_ids = self.env['purchase.reagent.groups'].sudo().search([('users_ids', 'in', self.env.uid)])
group_code = group_ids.shop_menu_type_id.code
url = shop_url.shop_url
url = url + '/' + group_code
print(url)
return url
注意:这里代码都是测试时候写的,比较难看,主要是像记录整个思路。