AI翻译及测试Odoo17开发教程,第二章:创建新应用,报错IndentationError unexpected indent的原因。

Chapter 2: A New Application

第2章:创建新应用

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

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

The purpose of this chapter is to lay the foundation for the creation of a completely new Odoo module. We will start from scratch with the minimum needed to have our module recognized by Odoo. In the upcoming chapters, we will progressively add features to build a realistic business case.

本章的目标是为了创建一个全新的Odoo模块奠定基础。我们将从零开始,仅使用让我们的模块被Odoo识别所需的最低要求出发。在接下来的章节里,我们将逐步增加功能,以构建一个贴近实际的商业应用场景。

The Real Estate Advertisement module

房地产广告模块

Our new module will cover a business area which is very specific and therefore not included in the standard set of modules: real estate. It is worth noting that before developing a new module, it is good practice to verify that Odoo doesn’t already provide a way to answer the specific business case.

我们的新模块将涉及一个非常特殊且因此未被标准模块集合所包含的业务领域:房地产。值得注意的是,在开发新模块之前,最好先确认Odoo是否已经提供了能满足该特定业务需求的解决方案。

Here is an overview of the main list view containing some advertisements:

以下是包含一些广告条目的主要列表视图概览:

List view 01

The top area of the form view summarizes important information for the property, such as the name, the property type, the postcode and so on. The first tab contains information describing the property: bedrooms, living area, garage, garden…

表单视图的顶部区域概括了房产的重要信息,如名称、房产类型、邮政编码等。第一个标签页包含了描述房产的具体信息:卧室数量、居住面积、车库、花园等。。。

The second tab lists the offers for the property. We can see here that potential buyers can make offers above or below the expected selling price. It is up to the seller to accept an offer.

第二个标签页列出了针对该房产的报价。我们可以看到,潜在买家可以提出高于或低于预期售价的报价。卖家则负责接受其中的一个报价。

Form view 02 表单视图02

Here is a quick video showing the workflow of the module.

下面是一个快速演示模块工作流程的视频。

Hopefully, this video will be recorded soon 🙂

希望这个视频能够尽快录制完成 🙂

Prepare the addon directory 准备插件目录

Reference: the documentation related to this topic can be found in manifest.

参考:与本主题相关的文档可在manifest中找到。

注解

Goal: the goal of this section is to have Odoo recognize our new module, which will be an empty shell for now. It will be listed in the Apps:

目标:本节的目标是让Odoo识别我们的新模块,目前它将是一个空白框架。它将会出现在“应用”列表中:

The new module appears in the list

The first step of module creation is to create its directory. In the tutorials directory, add a new directory estate.

创建模块的第一步是建立其目录。在教程目录中,新增一个名为estate的目录。

A module must contain at least 2 files: the __manifest__.py file and a __init__.py file. The __init__.py file can remain empty for now and we’ll come back to it in the next chapter. On the other hand, the __manifest__.py file must describe our module and cannot remain empty. Its only required field is the name, but it usually contains much more information.

一个模块至少需要包含两个文件:__manifest__.py文件和__init__.py文件。目前,__init__.py文件可以保持为空,我们将在下一章回过头来处理它。另一方面,__manifest__.py文件必须描述我们的模块,不能留空。它唯一必需的字段是模块名称,但通常会包含更多信息。

Take a look at the CRM file as an example. In addition to providing the description of the module (name, category, summary, website…), it lists its dependencies (depends). A dependency means that the Odoo framework will ensure that these modules are installed before our module is installed. Moreover, if one of these dependencies is uninstalled, then our module and any other that depends on it will also be uninstalled. Think about your favorite Linux distribution package manager (apt, dnf, pacman…): Odoo works in the same way.

以CRM文件为例。除了提供模块的描述(名称、类别、摘要、网站等)之外,它还列出了依赖项(depends)。依赖意味着Odoo框架会在安装我们模块之前确保这些模块已被安装。此外,如果这些依赖项中的任何一个被卸载,那么我们的模块以及任何依赖它的其他模块也将被卸载。想想你最喜欢的Linux发行版包管理器(apt、dnf、pacman…):Odoo的工作方式类似。

Exercise

Create the required addon files. 创建所需的插件文件。

Create the following folders and files: 创建以下文件夹和文件:

/home/$USER/src/tutorials/estate/__init__.py

/home/$USER/src/tutorials/estate/ __manifest__.py

The __manifest__.py file should only define the name and the dependencies of our modules. The only necessary framework module for now is base.

__manifest__.py文件应该只定义我们模块的名称和依赖项。目前所需的唯一框架模块是base。

Restart the Odoo server and go to Apps. Click on Update Apps List, search for estate and… tadaaa, your module appears! Did it not appear? Maybe try removing the default ‘Apps’ filter 😉

重启Odoo服务器并转到“应用”页面。点击“更新应用列表”,搜索“estate”然后……瞧!你的模块出现了!如果没出现?也许尝试移除默认的“应用”过滤器;-)

警告

Remember to enable the developer mode as explained in the previous chapter. You won’t see the Update Apps List button otherwise.

记得按照前一章所述启用开发者模式。否则,你将看不到“更新应用列表”按钮。

Exercise

Make your module an ‘App’. 使你的模块成为一个“应用”。

Add the appropriate key to your __manifest__.py so that the module appears when the ‘Apps’ filter is on.

在你的__manifest__.py中添加适当的键,以便当“应用”过滤器开启时模块能显示出来。

You can even install the module! But obviously it’s an empty shell, so no menu will appear.

你甚至可以安装这个模块!但显然它现在只是一个空壳,所以不会有菜单出现。

All good? If yes, then let’s create our first model!

一切顺利吗?如果是的话,那我们来创建我们的第一个模型吧!

实测中,报错了,IndentationError:unexpected indent 后来发现是 __manifest__.py 中 多放了个空行引起的:


评论

发表回复

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