odoo框架需求:假如有用户表订单表 现在需要让用户审核订单,审核人不能是订单表的申请人,这个方法如何实现请写出完整代码
时间: 2024-03-19 10:44:50 浏览: 51
Odoo 17插件: 如何实现销售人员仅能查看自己的联系人
在Odoo框架中实现该需求,需要进行以下步骤:
1. 定义用户表和订单表的模型
```python
from odoo import models, fields
class User(models.Model):
_name = 'myapp.user'
name = fields.Char()
email = fields.Char()
...
class Order(models.Model):
_name = 'myapp.order'
name = fields.Char()
user_id = fields.Many2one('myapp.user')
status = fields.Selection([('draft', 'Draft'), ('pending', 'Pending'), ('approved', 'Approved')], default='draft')
...
```
2. 创建订单审核模型,并继承订单模型
```python
class OrderApproval(models.Model):
_name = 'myapp.order_approval'
_inherits = {'myapp.order': 'order_id'}
order_id = fields.Many2one('myapp.order', required=True, ondelete='cascade')
approved_by = fields.Many2one('myapp.user', string='Approved By')
...
```
3. 定义订单审核流程
```python
class OrderWorkflow(models.Model):
_name = 'myapp.order_workflow'
def action_approve(self):
for rec in self:
if rec.order_id.status == 'pending' and rec.approved_by != rec.order_id.user_id:
rec.order_id.status = 'approved'
```
4. 将订单审核模型添加到菜单中
```xml
<odoo>
<data>
<menuitem id="menu_order_approval" name="Order Approval" parent="base.menu_sales" sequence="10"/>
<record id="view_order_approval_form" model="ir.ui.view">
<field name="name">Order Approval Form</field>
<field name="model">myapp.order_approval</field>
<field name="arch" type="xml">
<form string="Order Approval">
<sheet>
<group>
<field name="name"/>
<field name="user_id"/>
<field name="status"/>
<field name="approved_by"/>
...
</group>
</sheet>
</form>
</field>
</record>
<record id="action_order_approval" model="ir.actions.act_window">
<field name="name">Order Approval</field>
<field name="res_model">myapp.order_approval</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_order_approval_list" name="Order Approval" parent="menu_order_approval" action="action_order_approval"/>
</data>
</odoo>
```
5. 进行权限设置,使只有审核人才能操作
```xml
<odoo>
<data>
<record id="group_order_approval_user" model="res.groups">
<field name="name">Order Approval User</field>
</record>
<record id="rule_order_approval_access" model="ir.rule">
<field name="name">Order Approval Access</field>
<field name="model_id" ref="myapp.model_myapp_order_approval"/>
<field name="domain_force">[('approved_by', '=', user.id)]</field>
<field name="groups" eval="[(4,ref('myapp.group_order_approval_user'))]"/>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="False"/>
<field name="perm_unlink" eval="False"/>
</record>
</data>
</odoo>
```
以上是实现Odoo框架中的订单审核需求的完整代码,该代码具体实现了订单审核的业务逻辑和权限控制。
阅读全文