AI翻译及测试Odoo17开发教程,第九章:准备采取行动了吗?

Chapter 9: Ready For Some Action?
第九章:准备采取行动了吗?

原文来自:
https://www.odoo.com/documentation/17.0/zh_CN/developer/tutorials/server_framework_101/09_actions.html
使用通义千问翻译。

测试代码:https://gitee.com/zhang-wei918/estate

So far we have mostly built our module by declaring fields and views. We just introduced business logic in the previous chapter thanks to computed fields and onchanges. In any real business scenario, we would want to link some business logic to action buttons. In our real estate example, we would like to be able to:
到目前为止,我们主要通过声明字段和视图来构建我们的模块。在前一章中,我们通过计算字段和onchange事件引入了业务逻辑。在任何实际的商业场景中,我们都希望将一些业务逻辑与操作按钮联系起来。在我们的房地产示例中,我们希望能够:

  • cancel or set a property as sold 取消或设定房产为已售状态
  • accept or refuse an offer 接受或拒绝一个报价

One could argue that we can already do these things by changing the state manually, but this is not really convenient. Moreover, we want to add some extra processing: when an offer is accepted we want to set the selling price and the buyer for the property.
有人可能会争辩说,我们已经可以通过手动更改状态来做这些事情,但这并不是非常方便。此外,我们还想添加一些额外的处理:当一个报价被接受时,我们想要设定该房产的售价和买家。

Object Type 对象类型

Reference: the documentation related to this topic can be found in Actions and Error management.
参考:关于此主题的相关文档可以在《动作与错误管理》中找到。

 注解

Goal: at the end of this section: 目标:在本节结束时,

  • You should be able to cancel or set a property as sold:
    你应该能够取消或设定房产为已售状态:

Cancel and set to sold

A canceled property cannot be sold and a sold property cannot be canceled. For the sake of clarity, the state field has been added on the view.
一个被取消的房产不能被售出,而一个已售的房产不能被取消。为了清晰起见,状态字段已经被添加到视图上。

  • You should be able to accept or refuse an offer:
    你应该能够接受或拒绝一个报价:

Accept or refuse an offer

  • Once an offer is accepted, the selling price and the buyer should be set:
    一旦报价被接受,应设定售价和买家:

Accept an offer

In our real estate module, we want to link business logic with some buttons. The most common way to do this is to:
在我们的房地产模块中,我们希望将业务逻辑与一些按钮关联起来。最常见的方式是:

  • Add a button in the view, for example in the header of the view:
    在视图中添加一个按钮,例如,在视图的头部:
<form>
    <header>
        <button name="action_do_something" type="object" string="Do Something"/>
    </header>
    <sheet>
        <field name="name"/>
    </sheet>
</form>
  • and link this button to business logic: 并将此按钮与业务逻辑链接:
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

By assigning type="object" to our button, the Odoo framework will execute a Python method with name="action_do_something" on the given model.
通过将type=”object”分配给我们的按钮,Odoo框架将在给定模型上执行一个Python方法,其名称为”action_do_something”。

The first important detail to note is that our method name isn’t prefixed with an underscore (_). This makes our method a public method, which can be called directly from the Odoo interface (through an RPC call). Until now, all methods we created (compute, onchange) were called internally, so we used private methods prefixed by an underscore. You should always define your methods as private unless they need to be called from the user interface.
首先需要注意的重要细节是我们的方法名并未以下划线(_)开头。这使得我们的方法成为一个公共方法,可以直接从Odoo界面(通过RPC调用)调用。直到现在,我们创建的所有方法(计算、onchange)都是内部调用的,所以我们使用以下划线开头的私有方法。除非它们需要从用户界面调用,否则你应当始终将你的方法定义为私有的。

Also note that we loop on self. Always assume that a method can be called on multiple records; it’s better for reusability.
同样值得注意的是我们对self进行循环。始终假设一个方法可能在多条记录上调用;这对于可重用性更好。

Finally, a public method should always return something so that it can be called through XML-RPC. When in doubt, just return True.
最后,一个公共方法应该始终返回一些东西,以便它可以通过XML-RPC调用。当不确定时,只需返回True即可。

There are hundreds of examples in the Odoo source code. One example is this button in a view and its corresponding Python method
Odoo源代码中有成百上千的例子。一个例子是视图中的这个按钮及其对应的Python方法。

 Exercise

Cancel and set a property as sold. 取消和设定房产为已售。

  • Add the buttons ‘Cancel’ and ‘Sold’ to the estate.property model. A canceled property cannot be set as sold, and a sold property cannot be canceled.
    向estate.property模型添加‘取消’和‘售出’按钮。一个被取消的房产不能被设定为已售,而一个已售的房产不能被取消。

    Refer to the first image of the Goal for the expected result.
    参照目标的第一张图片以获得预期的结果。

    Tip: in order to raise an error, you can use the UserError function. There are plenty of examples in the Odoo source code 😉
    提示:为了引发错误,你可以使用UserError函数。Odoo源代码中有大量示例;-)
  • Add the buttons ‘Accept’ and ‘Refuse’ to the estate.property.offer model.
    向estate.property.offer模型添加‘接受’和‘拒绝’按钮。

    Refer to the second image of the Goal for the expected result.
    参照目标的第二张图片以获得预期的结果。

    Tip: to use an icon as a button, have a look at this example.
    提示:要使用图标作为按钮,请参阅此示例。
  • When an offer is accepted, set the buyer and the selling price for the corresponding property.
    当报价被接受时,为相应的房产设定买家和售价。

    Refer to the third image of the Goal for the expected result.
    参照目标的第三张图片以获得预期的结果。

    Pay attention: in real life only one offer can be accepted for a given property!
    请注意:在现实生活中,对于给定的房产,只能接受一个报价!

Action Type 动作类型

In Chapter 5: Finally, Some UI To Play With, we created an action that was linked to a menu. You may be wondering if it is possible to link an action to a button. Good news, it is! One way to do it is:
在第五章《终于,有些用户界面可以玩了》中,我们创建了一个与菜单关联的动作。你可能在想是否可以将一个动作链接到一个按钮上。好消息是,这是可以做到的!实现的一种方式是:

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

We use type="action" and we refer to the external identifier in the name.
我们使用type="action",并在name属性中引用外部标识符。

In the next chapter we’ll see how we can prevent encoding incorrect data in Odoo.
在下一章中,我们将了解如何防止在Odoo中输入不正确的数据。


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注