请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 83|回复: 0

[Python] odoo 开发入门教程系列-准备一些操作(Action)?

[复制链接]

2158

主题

0

回帖

2158

积分

金牌会员

积分
2158
威望
0
金币
4845
贡献
0
注册时间
2022-7-17
最后登录
2022-8-29
发表于 2023-4-2 19:36:00 | 显示全部楼层 |阅读模式

人人为我,我为人人。登录后免费下载所有资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
准备一些操作(Action)?
到目前为止,我们主要通过声明字段和视图来构建模块。在任何真实的业务场景中,我们都希望将一些业务逻辑链接到操作按钮。在我们的房地产示例中,我们希望能够:
  • 取消或将房产设置为已售出
  • 接受或拒绝报价

    有人可能会说,我们已经可以通过手动更改状态来完成这些事情,但这并不太方便。此外,我们还想增加一些额外的处理:当报价被接受时,我们想设定房产的售价和买家。
    操作类型(Action Type)
    参考:本主题相关文档可参考Actions 和Error management
    在我们的房地产模块中,我们希望将些业务逻辑和一些按钮关联,最常见的做法是:
  • 在视图中添加一个按钮,比如在视图
    1. header
    复制代码
    部分:

    1. form
    2.     header
    3.         button name="action_do_something" type="object" string="Do Something"/
    4.     /header
    5.     sheet
    6.         field name="name"/
    7.     /sheet
    8. /form
    复制代码

  • 将该按钮和业务逻辑关联:

    1. from odoo import fields, models
    2. class TestAction(models.Model):
    3.     _name = "test.action"
    4.     name = fields.Char()
    5.     def action_do_something(self):
    6.         for record in self:
    7.             record.name = "Something"
    8.         return True
    复制代码

    通过将
    1. type="object"
    复制代码
    分配给我们的按钮, Odoo框架将在给定模型上执行带有
    1. name="action_do_something"
    复制代码
    的Python方法。
    需要注意的第一个重要细节是,我们的方法名没有前缀下划线(
    1. _
    复制代码
    )。这使我们的方法成为一个公共方法,可以直接通过Odoo接口调用(通过RPC调用)。到目前为止,我们创建的所有方法(compute、onchange)都是在内部调用的,因此我们使用了前缀为下划线的私有方法。除非需要从用户界面调用方法,否则应始终将方法定义为私有。
    还要注意,我们对
    1. self
    复制代码
    循环。始终假设可以对多个记录调用同一个方法;这有利于重用性。
    最后,公共方法应该始终返回一些东西,以便可以通过XML-RPC调用它。当有疑问时,只需
    1. return True
    复制代码
    即可。
    Odoo源代码中有数百个示例。其中一个例子是 视图中的按钮 和其对应的Python方法
    1. form class="o_lead_opportunity_form" js_class="crm_form"
    2.     header
    3.         button name="action_set_won_rainbowman" string="Mark Won"
    4.             type="object" class="oe_highlight"
    5.             attrs="{'invisible': ['|','|', ('active','=',False), ('probability', '=', 100), ('type', '=', 'lead')]}"/
    6.         ...略
    复制代码
    1.     def action_set_won_rainbowman(self):
    2.         self.ensure_one()
    3.         self.action_set_won()
    4.         message = self._get_rainbowman_message()
    5.         if message:
    6.             return {
    7.                 'effect': {
    8.                     'fadeout': 'slow',
    9.                     'message': message,
    10.                     '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',
    11.                     'type': 'rainbow_man',
    12.                 }
    13.             }
    14.         return True
    复制代码

    练习1
    添加 ‘Cancel’ 和‘Sold’ 按钮到
    1. estate.property
    复制代码
    模型。已取消的房产不能被设置为已出售,已出售的房产不能被取消。
    预期效果动画:

    1569452-20230324210935350-2026001039.gif

    1569452-20230324210935350-2026001039.gif


    1569452-20230324210954676-1238345899.gif

    1569452-20230324210954676-1238345899.gif

    提示:为了抛出错误,可以使用  UserError 函数。
    修改
    1. odoo14\custom\estate\views\estate_property_views.xml
    复制代码
    中的
    1. estate_property_view_form
    复制代码
    视图
    1.     record id="estate_property_view_form" model="ir.ui.view"
    2.         field name="name"estate.property.form/field
    3.         field name="model"estate.property/field
    4.         field name="arch" type="xml"
    5.             form string="estate property form"
    6.                 !-- header元素为本次新增 --
    7.                 header
    8.                     button name="set_property_sold" type="object" string="SOLD"/button                    
    9.                     button name="set_property_canceled" type="object" string="CANCEL"/button
    10.                 /header
    11.                 sheet
    12.                     h1
    13.                         field name="name"/
    14.                     /h1
    15.                     p
    16.                         field name="tag_ids" widget="many2many_tags"/
    17.                     /p
    18.                     group
    19.                         group
    20.                             !-- state 字段为本次新增 --
    21.                             field name="state" string="Status"/field
    22.                             field name="property_type_id" string="Property Type"/field
    23.                             field name="postcode" string="Postcode" /field
    24.                             field name="date_availability" string="Available From"/field
    25.                         /group
    26.                         group
    27.                             field name="expected_price" string="Expected Price"/field
    28.                             field name="best_price" string="Best Price" /
    29.                             field name="selling_price" string="Selling Price"/field
    30.                         /group
    31.                     /group
    32.                     notebook
    33.                         page string="Description"
    34.                             group
    35.                                 field name="description"/field
    36.                                 field name="bedrooms"/field
    37.                                 field name="living_area"/field
    38.                                 field name="facades"/field
    39.                                 field name="garage"/field
    40.                                 field name="garden"/field
    41.                                 field name="garden_area"/field
    42.                                 field name="garden_orientation"/field
    43.                                 field name="total_area" string="Total Area"/field
    44.                             /group
    45.                         /page
    46.                         page string="Offers"
    47.                             field name="offer_ids" /
    48.                         /page
    49.                         page string="Other info"
    50.                             group
    51.                                 field name="salesman_id" string="Salesman"/field
    52.                                 field name="buyer_id" string="Buyer"/field
    53.                             /group
    54.                         /page
    55.                     /notebook
    56.                 /sheet
    57.             /form
    58.         /field
    59.     /record
    复制代码

    修改
    1. odoo14\custom\estate\models\estate_property.py
    复制代码

    开头增加导入
    1. UserError
    复制代码
    1. from odoo.exceptions import UserError
    复制代码

    末尾新增以下代码
    1.     def set_property_canceled(self):
    2.         if self.state == 'Sold':
    3.             raise UserError('不能取消已出售房产')
    4.         else:
    5.             self.state = 'Canceled'
    6.         return True
    7.     def set_property_sold(self):
    8.         if self.state == 'Canceled':
    9.             raise UserError('不能出售已取消房产')
    10.         else:
    11.             self.state = 'Sold'
    12.         return True
    复制代码

    重启服务,浏览器中验证
    练习2
    添加‘Accept’ 和‘Refuse’ 到
    1. estate.property.offer
    复制代码
    模型。
    预期效果动画:

    1569452-20230324211018162-85789158.gif

    1569452-20230324211018162-85789158.gif


    1569452-20230324211040371-1603410142.gif

    1569452-20230324211040371-1603410142.gif

    提示: 把图标当按钮用,请查看这个例子
    1. button name="action_confirm" string="Confirm" states="draft" type="object" icon="fa-check"/
    复制代码

    修改
    1. odoo14\custom\estate\views\estate_property_offer_views.xml
    复制代码
    1. estate_property_offer_view_tree
    复制代码
    1.     record id="estate_property_offer_view_tree" model="ir.ui.view"
    2.         field name="name"estate.property.offer.tree/field
    3.         field name="model"estate.property.offer/field
    4.         field name="arch" type="xml"
    5.             tree string="PropertyOffers"
    6.                 field name="price" string="Price"/
    7.                 field name="partner_id" string="partner ID"/
    8.                 field name="validity" string="Validity(days)"/
    9.                 field name="deadline" string="Deadline"/
    10.                 !-- button 为本次新增 --
    11.                 button name="action_accept_offer" string=""  type="object" icon="fa-check"/
    12.                 button name="action_refuse_offer" string=""  type="object" icon="fa-times"/
    13.                 field name="status" string="Status"/
    14.             /tree
    15.         /field
    16.     /record
    复制代码

    修改
    1. odoo14\custom\estate\models\estate_property_offer.py
    复制代码
    ,最末尾添加以下代码
    1.     def action_accept_offer(self):
    2.         self.status = 'Accepted'
    3.         self.property_id.state = 'Offer Accepted'
    4.         return True
    5.     def action_refuse_offer(self):
    6.         self.status = 'Refused'
    7.         return True
    复制代码

    重启服务,浏览器中验证

    1569452-20230324211106242-199764773.png

    1569452-20230324211106242-199764773.png

    练习3
    当报价被接受时,设定相应房产的买家和售价。
    预期效果动画:

    1569452-20230324211132974-36167678.gif

    1569452-20230324211132974-36167678.gif

    注意:在现实生活中,给定房产只能接受一个报价!
    修改
    1. odoo14\custom\estate\models\estate_property_offer.py
    复制代码
    1. action_accept_offer
    复制代码
    函数如下
    1.     def action_accept_offer(self):
    2.         self.status = 'Accepted'
    3.         self.property_id.state = 'Offer Accepted'
    4.         self.property_id.selling_price = 260000
    5.         self.property_id.buyer_id = self.partner_id
    6.         return True
    复制代码

    重启服务,浏览器中验证
    对象类型(Object Type)
    “一些用户界面”章节中,我们创建了连接到菜单的操作。你可能好奇,是否可以连接操作到按钮。好消息,的确可以,其中一种实现方式如下:
    1. button type="action" name="%(test.test_model_action)d" string="My Action"/
    复制代码

    我们使用
    1. type="action"
    复制代码
    且在
    1. name
    复制代码
    中引用外部标识
  • 我爱编程论坛www.woaibiancheng.cn
    回复

    使用道具 举报

    侵权举报|手机版|我爱编程论坛 ( 蜀ICP备2022018035号-1 )

    GMT+8, 2023-10-3 21:37

    Powered by Discuz! X3.5

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表