准备一些操作(Action)?

到目前为止,我们主要通过声明字段和视图来构建模块。在任何真实的业务场景中,我们都希望将一些业务逻辑链接到操作按钮。在我们的房地产示例中,我们希望能够:

  • 取消或将房产设置为已售出
  • 接受或拒绝报价

有人可能会说,我们已经可以通过手动更改状态来完成这些事情,但这并不太方便。此外,我们还想增加一些额外的处理:当报价被接受时,我们想设定房产的售价和买家。

操作类型(Action Type)

参考:本主题相关文档可参考ActionsError management

在我们的房地产模块中,我们希望将些业务逻辑和一些按钮关联,最常见的做法是:

  • 在视图中添加一个按钮,比如在视图header部分:
<form>
<header>
<button name="action_do_something" type="object" string="Do Something"/>
</header>
<sheet>
<field name="name"/>
</sheet>
</form>
  • 将该按钮和业务逻辑关联:
from odoo import fields, models

class TestAction(models.Model):
_name = "test.action" name = fields.Char() def action_do_something(self):
for record in self:
record.name = "Something"
return True

通过将type="object"分配给我们的按钮, Odoo框架将在给定模型上执行带有name="action_do_something"的Python方法。

需要注意的第一个重要细节是,我们的方法名没有前缀下划线(_)。这使我们的方法成为一个公共方法,可以直接通过Odoo接口调用(通过RPC调用)。到目前为止,我们创建的所有方法(compute、onchange)都是在内部调用的,因此我们使用了前缀为下划线的私有方法。除非需要从用户界面调用方法,否则应始终将方法定义为私有。

还要注意,我们对self循环。始终假设可以对多个记录调用同一个方法;这有利于重用性。

最后,公共方法应该始终返回一些东西,以便可以通过XML-RPC调用它。当有疑问时,只需return True即可。

Odoo源代码中有数百个示例。其中一个例子是 视图中的按钮 和其对应的Python方法

<form class="o_lead_opportunity_form" js_class="crm_form">
<header>
<button name="action_set_won_rainbowman" string="Mark Won"
type="object" class="oe_highlight"
attrs="{'invisible': ['|','|', ('active','=',False), ('probability', '=', 100), ('type', '=', 'lead')]}"/>
...略
    def action_set_won_rainbowman(self):
self.ensure_one()
self.action_set_won() message = self._get_rainbowman_message()
if message:
return {
'effect': {
'fadeout': 'slow',
'message': message,
'img_url': '/web/image/%s/%s/image_1024' % (self.team_id.user_id._name, self.team_id.user_id.id) if self.team_id.user_id.image_1024 else '/web/static/src/img/smile.svg',
'type': 'rainbow_man',
}
}
return True

练习1

添加 ‘Cancel’ 和‘Sold’ 按钮到 estate.property 模型。已取消的房产不能被设置为已出售,已出售的房产不能被取消。

预期效果动画:

提示:为了抛出错误,可以使用 UserError 函数。

修改odoo14\custom\estate\views\estate_property_views.xml中的estate_property_view_form视图

    <record id="estate_property_view_form" model="ir.ui.view">
<field name="name">estate.property.form</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<form string="estate property form">
<!-- header元素为本次新增 -->
<header>
<button name="set_property_sold" type="object" string="SOLD"></button>
<button name="set_property_canceled" type="object" string="CANCEL"></button>
</header>
<sheet>
<h1>
<field name="name"/>
</h1>
<p>
<field name="tag_ids" widget="many2many_tags"/>
</p>
<group>
<group>
<!-- state 字段为本次新增 -->
<field name="state" string="Status"></field>
<field name="property_type_id" string="Property Type"></field>
<field name="postcode" string="Postcode" ></field>
<field name="date_availability" string="Available From"></field>
</group>
<group>
<field name="expected_price" string="Expected Price"></field>
<field name="best_price" string="Best Price" />
<field name="selling_price" string="Selling Price"></field>
</group>
</group>
<notebook>
<page string="Description">
<group>
<field name="description"></field>
<field name="bedrooms"></field>
<field name="living_area"></field>
<field name="facades"></field>
<field name="garage"></field>
<field name="garden"></field>
<field name="garden_area"></field>
<field name="garden_orientation"></field>
<field name="total_area" string="Total Area"></field>
</group>
</page>
<page string="Offers">
<field name="offer_ids" />
</page>
<page string="Other info">
<group>
<field name="salesman_id" string="Salesman"></field>
<field name="buyer_id" string="Buyer"></field>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>

修改odoo14\custom\estate\models\estate_property.py

开头增加导入UserError

from odoo.exceptions import UserError

末尾新增以下代码

    def set_property_canceled(self):
if self.state == 'Sold':
raise UserError('不能取消已出售房产')
else:
self.state = 'Canceled' return True def set_property_sold(self):
if self.state == 'Canceled':
raise UserError('不能出售已取消房产')
else:
self.state = 'Sold' return True

重启服务,浏览器中验证

练习2

添加‘Accept’ 和‘Refuse’ 到estate.property.offer 模型。

预期效果动画:

提示: 把图标当按钮用,请查看这个例子

<button name="action_confirm" string="Confirm" states="draft" type="object" icon="fa-check"/>

修改odoo14\custom\estate\views\estate_property_offer_views.xmlestate_property_offer_view_tree

    <record id="estate_property_offer_view_tree" model="ir.ui.view">
<field name="name">estate.property.offer.tree</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<tree string="PropertyOffers">
<field name="price" string="Price"/>
<field name="partner_id" string="partner ID"/>
<field name="validity" string="Validity(days)"/>
<field name="deadline" string="Deadline"/>
<!-- button 为本次新增 -->
<button name="action_accept_offer" string="" type="object" icon="fa-check"/>
<button name="action_refuse_offer" string="" type="object" icon="fa-times"/>
<field name="status" string="Status"/>
</tree>
</field>
</record>

修改odoo14\custom\estate\models\estate_property_offer.py,最末尾添加以下代码

    def action_accept_offer(self):
self.status = 'Accepted'
self.property_id.state = 'Offer Accepted'
return True def action_refuse_offer(self):
self.status = 'Refused'
return True

重启服务,浏览器中验证

练习3

当报价被接受时,设定相应房产的买家和售价。

预期效果动画:

注意:在现实生活中,给定房产只能接受一个报价!

修改odoo14\custom\estate\models\estate_property_offer.pyaction_accept_offer函数如下

    def action_accept_offer(self):
self.status = 'Accepted'
self.property_id.state = 'Offer Accepted'
self.property_id.selling_price = 260000
self.property_id.buyer_id = self.partner_id
return True

重启服务,浏览器中验证

对象类型(Object Type)

“一些用户界面”章节中,我们创建了连接到菜单的操作。你可能好奇,是否可以连接操作到按钮。好消息,的确可以,其中一种实现方式如下:

<button type="action" name="%(test.test_model_action)d" string="My Action"/>

我们使用 type="action" 且在name中引用外部标识

odoo 开发入门教程系列-准备一些操作(Action)?的更多相关文章

  1. SeaJS入门教程系列之使用SeaJS(二)

    SeaJS入门教程系列之使用SeaJS(二) 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-03我要评论 这篇文章主要介绍了SeaJS入门教程系列之使用SeaJS,着重介绍了SeaJ ...

  2. 移动H5开发入门教程:12点webAPP前端开发经验

    如果你是一名移动H5前端开发人员,25学堂的小编认为下面的分享的12点webAPP前端开发经验是你必须掌握的基础知识点.算是一篇移动H5开发入门教程吧! 1. viewport:也就是可视区域.对于桌 ...

  3. ActiveMQ详细入门教程系列(一)

    一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...

  4. WPF入门教程系列二——Application介绍

    一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...

  5. 基于Nodejs生态圈的TypeScript+React开发入门教程

    基于Nodejs生态圈的TypeScript+React开发入门教程   概述 本教程旨在为基于Nodejs npm生态圈的前端程序开发提供入门讲解. Nodejs是什么 Nodejs是一个高性能Ja ...

  6. iOS开发入门教程

    iOS开发入门教程 http://my.oschina.net/mailzwj/blog/133273 摘要 iOS开发入门教程,从创建项目到运行项目,包括OC基础,调试,模拟器设置等相关知识. iO ...

  7. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...

  8. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...

  9. ENVI Services Engine5.1 应用开发入门教程

    原文地址: ENVI Services Engine5.1 应用开发入门教程_ENVI-IDL中国_新浪博客 http://blog.sina.com.cn/s/blog_764b1e9d0102uy ...

  10. Android Studio JNI开发入门教程

    Android Studio JNI开发入门教程 2016-08-29 14:38 3269人阅读 评论(0) 收藏 举报  分类: JNI(3)    目录(?)[+]   概述 在Andorid ...

随机推荐

  1. JavaSSM

    Day1221 一.IT行业分类 前端 用户界面,眼睛能看到的,视觉效果比较. html5.css和css3.javascript.jquery.技术基础 bootstrap(css框架).vue.j ...

  2. 使用 nvm 管理 node.js 版本

    简介 在实际的前端开发过程中,可能会经常遇见 node.js 的版本问题,不同的项目需要使用不同的 node.js 版本. 直接安装的话,只能安装和使用 node.js 的一个版本.可以使用 nvm ...

  3. Python 使用json存储数据

    一.前言 很多程序都要求用户输入某种信息,如让用户存储游戏首选项或提供要可视化的数据.不管专注的是什么,程序都把用户提供的信息存储在列表和字典等数据结构中.用户关闭程序时,你几乎总是要保存他们提供的信 ...

  4. seleniumUI自动化学习记录

    2019.2.9 尝试了一个启动浏览器并打开指定网址的程序: 这里首先要注意的就是浏览器的版本和selenium jar包的版本必须符合才行,不然会报错 2019.9.16 必须要下载相应的chrom ...

  5. python 添加文件模板,默认添加作者时间等必要信息

    1.模板设置 配置路径:Setting-Editor-File and Code Templates-Python-Script 2.示例输入 代码如下(示例): #!/usr/bin/env pyt ...

  6. ST能维护的性质

    总结: 其实ST表不仅能处理最大值/最小值,凡是符合结合律且可重复贡献的信息查询都可以使用ST表高效进行.什么叫可重复贡献呢?设有一个二元运算  ,满足  ,则是可重复贡献的.显然最大值.最小值.最大 ...

  7. 实验一 Linux系统与应用课程准备

    項目 內容 这个作业属于哪个课程 班级课程主要链接 这个作业的要求在哪里 作业要求链接 学号-姓名 15043109-吴小怀 作业学习目标 学会在博客园社区中学习Linux的使用技巧,熟练使用Typo ...

  8. MySQL_20200417

    MySQL安装与卸载 SQL语句与Oracle大致相同 主要使用 Navicat for MySQL进行数据库操作 MySQL常用的命令: 登录:mysql -uroot -p  /mysql -ur ...

  9. SQLyog 13.1.1.0注册码证书秘钥

    注册信息: Name:(用户名随意) License Key: Professional: 8e053a86-cdd3-48ed-b5fe-94c51b3d343c Enterprise: a4668 ...

  10. C++ || 用类 写交换函数 ||函数指针传递

    点击查看代码 #include <iostream> using namespace std; void swap(int* a,int* b) //函数参数为指针形式 { int p = ...