命令行界面(CLI)

CLI 命令行界面 提供了与 Odoo 相关的多个功能。您可以使用它来 运行服务器启动 Odoo 作为 Python 控制台 环境创建一个 Odoo 模块填充数据库 ,或者 计算代码行数

重要

The command to use to call the CLI depends on how you installed Odoo. In the examples below, we assume that you are running Odoo from source with the odoo-bin file. If you installed Odoo from a distribution package or with Docker, you must adapt the command.

  1. 进入您下载的Odoo社区源代码文件的根目录。

  2. 使用 ./odoo-bin 运行所有 CLI 命令

帮助和版本

-h, --help

显示所有可用选项的帮助文本

--version

显示Odoo版本,例如”Odoo Server 16.0”

小技巧

您可以通过运行以下命令在您的 shell 中启用自动补全功能

echo "complete -W '`./odoo-bin --help | \
  sed -e 's/[^a-z_-]\(-\+[a-z0-9_-]\+\)/\n\1\n/' | \
  grep -- '^-' | sort | uniq | tr '\n' ' '`' odoo-bin" >> ~/.bash_completion

运行服务器

-d <database>, --database <database>

在安装或更新模块时使用的数据库。提供逗号分隔的列表可限制访问提供的数据库。

对于高级数据库选项,请参考 下面.

-i <modules>, --init <modules>

在运行服务器之前安装的模块的逗号分隔列表(需要 -d)。

-u <modules>, --update <modules>

逗号分隔的模块列表,在运行服务器之前更新。使用 all 来更新所有模块。(需要 -d)。

--addons-path <directories>

逗号分隔的目录列表,其中存储了模块。这些目录将被扫描以查找模块。

--upgrade-path <upgrade_path>

指定一个额外的升级路径。

--load <modules>

要加载的服务器级模块列表。这些模块应该提供与特定数据库无关的功能。这与在安装时始终绑定到特定数据库的模块(即大多数Odoo插件)形成对比。默认值为 base,web

-c <config>, --config <config>

指定一个备用的 配置文件路径。如果未定义,Odoo 会检查 ODOO_RC 环境变量和默认位置 $HOME/.odoorc。请参阅配置文件部分 下面

-D <data-dir-path>, --data-dir <data-dir-path>

存储Odoo数据的目录路径(例如,文件存储,会话)。如果未指定,Odoo将回退到预定义的路径。在Unix系统中,它是在``$XDG_DATA_HOME``环境变量中定义的,或者是``~/.local/share/Odoo``或``/var/lib/Odoo``文件。

-s, --save

将服务器配置保存到当前配置文件 (默认为 $HOME/.odoorc,可以使用 -c 进行覆盖)。

--without-demo

禁用已安装模块的演示数据加载,使用逗号分隔,使用 all 表示所有模块。需要 -d-i

--pidfile=<pidfile>

服务器 PID 存储的文件路径

--stop-after-init

在初始化后停止服务器。

--geoip-db <path>

GeoIP数据库文件的绝对路径。

测试配置

--test-enable

在模块安装后运行测试

--test-file <file>

运行一个 Python 测试文件

--test-tags [-][tag][/module][:class][.method]

逗号分隔的规范列表,用于过滤要执行的测试。如果设置,则启用单元测试。

示例: --test-tags :TestClass.test_func,/test_module,external

  • “-” 指定我们是否要包含或排除与此规范匹配的测试。

  • 该标签将匹配使用 tagged() 装饰器添加到类上的标签(所有 测试类 在明确删除之前都具有 standardat_install 标签,请参阅装饰器文档)。

  • * will match all tags.

  • 如果在包含模式下省略了标签,则其值为 standard

  • 如果在排除模式下省略了标签,则其值为 *

  • 模块、类和方法将分别匹配模块名称、测试类名称和测试方法名称。

测试的筛选和执行会发生两次:在每个模块安装/更新之后和模块加载结束时。在每个阶段,测试都会通过 --test-tags 规范进行筛选,并通过动态规范 at_installpost_install 进一步筛选。

--screenshots

指定目录,用于在 HttpCase.browser_js 测试失败时写入截图。默认为 /tmp/odoo_tests/db_name/screenshots

--screencasts

启用屏幕录制并指定写入屏幕录制文件的目录。需要安装 ffmpeg 实用程序将帧编码为视频文件。否则,帧将被保留而不是视频文件。

数据库

-r <user>, --db_user <user>

数据库用户名,用于连接到 PostgreSQL。

-w <password>, --db_password <password>

数据库密码,如果使用 password authentication

--db_host <hostname>

数据库服务器的主机

  • Windows 上的 localhost

  • 否则为UNIX套接字

--db_port <port>

数据库监听的端口,默认为5432

--db-filter <filter>

隐藏不匹配 <filter> 的数据库。过滤器是一个 正则表达式,附加了以下内容:

  • %h is replaced by the whole hostname the request is made on.

  • %d 被替换为发出请求的子域名,除了 www``(所以域名 ``odoo.comwww.odoo.com 都匹配数据库 odoo)。

    这些操作区分大小写。在匹配所有数据库时添加选项 (?i) (因此域名 odoo.com 使用 (?i)%d 可以匹配数据库 Odoo)。

自版本11起,还可以使用–database参数并指定逗号分隔的数据库列表来限制对给定数据库的访问权限

当结合这两个参数时,db-filter 优先于逗号分隔的数据库列表来限制数据库列表,而逗号分隔的列表用于执行所请求的操作,如模块升级。

$ odoo-bin --db-filter ^11.*$

限制访问以11开头的数据库

$ odoo-bin --database 11firstdatabase,11seconddatabase

限制访问仅限于两个数据库,11firstdatabase 和 11seconddatabase

$ odoo-bin --database 11firstdatabase,11seconddatabase -u base

限制访问仅限于两个数据库,11firstdatabase和11seconddatabase,并在一个数据库11firstdatabase上更新基础模块。如果数据库11seconddatabase不存在,则创建该数据库并安装基础模块。

$ odoo-bin --db-filter ^11.*$ --database 11firstdatabase,11seconddatabase -u base

限制访问以11开头的数据库,并在一个名为11firstdatabase的数据库上更新基础模块。如果11seconddatabase数据库不存在,则创建该数据库并安装基础模块。

--db-template <template>

在从数据库管理界面创建新数据库时,使用指定的 模板数据库。默认为 template0

--pg_path </path/to/postgresql/binaries>

用于数据库管理器转储和恢复数据库的 PostgreSQL 二进制文件的路径。只有在这些二进制文件位于非标准目录中时,才需要指定此选项。

--no-database-list

禁止列出系统上可用的数据库

--db_sslmode

控制Odoo和PostgreSQL之间连接的SSL安全性。值应为’disable’、’allow’、’prefer’、’require’、’verify-ca’或’verify-full’。默认值为’prefer’

--unaccent

Try to enable the unaccent extension when creating new databases

电子邮件

--email-from <address>

当Odoo需要发送邮件时,用作<FROM>的电子邮件地址

--from-filter <address or domain>

定义SMTP配置将应用于哪个电子邮件地址。该字段可以是域名或完整的电子邮件地址,也可以为空。如果发件人的电子邮件地址与此设置的过滤器不匹配,则将使用两个系统参数的组合来封装电子邮件: mail.default.frommail.catchall.domain 。例如,”Admin” <admin@example.com> => “Admin” <notifications@mycompany.com>。

--smtp <server>

连接以发送邮件的SMTP服务器的地址

--smtp-port <port>
--smtp-ssl

如果设置,Odoo应该使用SSL/STARTSSL SMTP连接

--smtp-user <name>

连接 SMTP 服务器的用户名

--smtp-password <password>

连接SMTP服务器的密码

--smtp-ssl-certificate-filename <path/to/cert.pem>

将使用 SSL 证书进行身份验证。如果设置,则需要 smtp-ssl-private-key

--smtp-ssl-private-key-filename <path/to/key.pem>

使用 SSL 私钥进行身份验证。如果设置,则需要 smtp-ssl-certificate

国际化

使用这些选项将Odoo翻译成另一种语言。请参阅用户手册中的i18n部分。选项“-d”是必需的。在导入的情况下,“-l”选项是必需的。

--load-language <languages>

指定您想要加载的翻译语言(用逗号分隔)

-l, --language <language>

指定翻译文件的语言。与 –i18n-export 或 –i18n-import 一起使用。

--i18n-export <filename>

将所有待翻译的句子导出到 CSV 文件、PO 文件或 TGZ 压缩包中并退出。

--i18n-import <filename>

导入包含翻译的 CSV 或 PO 文件并退出。必须使用 ‘-l’ 选项。

--i18n-overwrite

在更新模块或导入CSV或PO文件时覆盖现有的翻译术语。

--modules

指定要导出的模块。与 –i18n-export 一起使用。

高级选项

开发人员功能

--dev <feature,feature,...,feature>

逗号分隔的功能列表。仅供开发目的使用,请勿在生产中使用。可能的功能包括:

  • all: all the features below are activated

  • xml: read QWeb template from xml file directly instead of database. Once a template has been modified in database, it will be not be read from the xml file until the next update/init. Particularly, templates are not translated on using this option.

  • reload: restart server when python file are updated (may not be detected depending on the text editor used)

  • qweb:在节点包含``t-debug=’debugger’``时中断QWeb模板的评估

  • (i)p(u)db: start the chosen python debugger in the code when an unexpected error is raised before logging and returning the error.

  • werkzeug: display the full traceback on the frontend page in case of exception

HTTP

--no-http

不要启动HTTP或长轮询工作进程(可能仍然启动 cron 工作进程)

警告

如果设置了 --test-enable,则此选项无效,因为测试需要一个可访问的 HTTP 服务器

--http-interface <interface>

HTTP 服务器监听的 TCP/IP 地址,默认为 0.0.0.0 (所有地址)

-p <port>
--http-port <port>

HTTP服务器监听的端口,默认为8069。

--gevent-port <port>

在多进程或协程模式下用于 WebSocket 连接的 TCP 端口,默认为 8072。在默认(线程化)模式下不使用。

--proxy-mode

enables the use of X-Forwarded-* headers through Werkzeug’s proxy support.

It ignores all X-Forwarded-* headers in case X-Forwarded-Host is missing from the request.

It always gets the real IP from the last entry of the X-Forwarded-For chain. Configure your web server accordingly using directives such as nginx’s set_real_ip_from in case there are other trusted proxies along the chain that must be ignored.

X-Forwarded-Proto and X-Forwarded-Host are used to update the request root URL, which in turn is used to update the web.base.url system parameter upon a successful admin authentication. This system parameter is used to generate all links for the current database; see 数据库的Web基本URL.

警告

代理模式 绝不能 在非反向代理场景下启用

--x-sendfile

将附件文件的服务委托给静态 Web 服务器,并在流响应上设置 X-Sendfile (apache) 和 X-Accel-* (nginx) 的 HTTP 标头。有关 Web 服务器配置,请参见 提供静态文件和附件服务

日志记录

By default, Odoo displays all logging of level INFO, WARNING and ERROR. All logs independently of the level are output on stderr. Various options are available to redirect logging to other destinations and to customize the verbosity.

--logfile <file>

sends logging output to the specified file instead of stderr. On Unix, the file can be managed by external log rotation programs and will automatically be reopened when replaced

--syslog

logs to the system’s event logger: syslog on unices and the Event Log on Windows.

两者都不可配置

--log-db <dbname>

将日志记录到指定数据库的``ir.logging``模型(``ir_logging``表)。数据库可以是当前PostgreSQL中的数据库名称,也可以是用于日志聚合的`PostgreSQL URI`_。

--log-handler <handler-spec>

LOGGER:LEVEL, enables LOGGER at the provided LEVEL e.g. odoo.models:DEBUG will enable all logging messages at or above DEBUG level in the models.

  • 冒号 : 是必须的

  • 可以省略记录器以配置根(默认)处理程序

  • 如果省略了级别,则将记录器设置为 INFO

该选项可以重复使用以配置多个记录器,例如:

$ odoo-bin --log-handler :DEBUG --log-handler werkzeug:CRITICAL --log-handler odoo.fields:WARNING
--log-web

启用 HTTP 请求和响应的 DEBUG 日志记录,相当于 --log-handler=odoo.http:DEBUG

--log-sql

启用SQL查询的DEBUG日志记录,相当于 --log-handler=odoo.sql_db:DEBUG

--log-level <level>

更容易地设置特定记录器的预定义级别的快捷方式。”真实”级别(criticalerrorwarndebug)设置在``odoo``和``werkzeug``记录器上(除了``debug``只设置在``odoo``上)。

Odoo还提供了针对不同记录器应用的调试伪级别:

debug_sql

将 SQL 日志记录器设置为 debug

等同于 --log-sql

debug_rpc

odoo 和 HTTP 请求日志记录器设置为 debug

等同于 --log-level debug --log-request

debug_rpc_answer

odoo 和 HTTP 请求和响应的日志记录器设置为 debug

等同于 --log-level debug --log-request --log-response

注解

--log-level--log-handler 之间发生冲突时,后者会被使用

多进程

--workers <count>

如果 count 不为 0(默认值),则启用多进程并设置指定数量的 HTTP 工作进程(子进程处理 HTTP 和 RPC 请求)。

注解

多进程模式仅适用于基于Unix的系统

许多选项允许限制和回收工作进程:

--limit-request <limit>

工作进程在被回收和重新启动之前将处理的请求数。

默认为 8196

--limit-memory-soft <limit>

Maximum allowed virtual memory per worker in bytes. If the limit is exceeded, the worker is killed and recycled at the end of the current request.

Defaults to 2048MiB (2048*1024*1024B).

--limit-memory-hard <limit>

Hard limit on virtual memory in bytes, any worker exceeding the limit will be immediately killed without waiting for the end of the current request processing.

Defaults to 2560MiB (2560*1024*1024B).

--limit-time-cpu <limit>

防止工作进程在每个请求中使用超过<limit> CPU秒。如果超过限制,工作进程将被终止。

默认为 60

--limit-time-real <limit>

防止工作进程处理请求的时间超过<limit>秒。如果超过限制,工作进程将被终止。

--limit-time-cpu 不同之处在于这是一个包括 SQL 查询在内的 “wall time” 限制。

默认为 120

--max-cron-threads <count>

用于 cron 作业的工作进程数量。默认为 2。这些工作进程在多线程模式下是线程,在多进程模式下是进程。

对于多进程模式,这是HTTP工作进程的补充。

配置文件

大多数命令行选项也可以通过配置文件指定。大多数情况下,它们使用类似的名称,前缀 - 被移除,其他 - 被替换为 _ ,例如: --db-template 变成了 db_template

一些转换不符合模式:

默认的配置文件是 $HOME/.odoorc,可以使用 --config 来覆盖。使用 --save 可以将当前的配置状态保存回该文件。与命令行相关的配置项应在 [options] 部分中指定。

这是一个示例文件:

[options]
db_user=odoo
dbfilter=odoo

命令行

The Odoo command line also allows launching Odoo as a Python console environment, enabling direct interaction with the orm and its functionalities.

$ odoo-bin shell

Example

Adding an exclamation mark to all contacts’ names:

In [1]: records = env["res.partner"].search([])

In [2]: records
Out[2]: res.partner(14, 26, 33, 21, 10)

In [3]: for partner in records:
   ...:     partner.name = "%s !" % partner.name
   ...:

In [4]: env.cr.commit()

重要

By default, the shell is running in transaction mode. This means that any change made to the database is rolled back when exiting the shell. To commit changes, use env.cr.commit().

--shell-interface (ipython|ptpython|bpython|python)

Specify a preferred REPL to use in shell mode. This shell is started with the env variable already initialized to be able to access the ORM and other Odoo modules.

另请参阅

环境

脚手架

Scaffolding是自动创建骨架结构以简化引导(在Odoo的情况下是新模块)的过程。虽然不是必需的,但它避免了设置基本结构和查找所有起始要求的繁琐过程。

通过 odoo-bin scaffold 子命令可以使用脚手架。

$ odoo-bin scaffold my_module /addons/
name (required)

要创建的模块名称,可以以各种方式进行修改以生成编程名称(例如,模块目录名称,模型名称等)

destination (default=current directory)

要创建新模块的目录,默认为当前目录

-t <template>

一个模板目录,文件会经过 jinja2 处理后复制到 destination 目录

这将在目录 /addons/ 中创建模块 my_module

数据库填充

Odoo CLI 支持数据库填充功能。如果该功能在给定模型上 实现了,它允许自动生成模型记录的数据,以便在包含大量记录的数据库中测试您的模块。

$ odoo-bin populate
--models

应填充数据库的模型列表

--size (small|medium|large)

population size, the actual records number depends on the model’s _populate_sizes attribute. The generated records content is specified by the _populate_factories() method of a given model (cf. the populate folder of modules for further details).

另请参阅

数据库填充

Cloc

Odoo Cloc 是一种工具,用于计算Python、JavaScript、CSS、SCSS或XML中编写的相关行数。这可以用作额外模块维护定价的粗略度量。

命令行选项

-d <database>, --database <database>
处理提供的数据库中安装的所有额外模块的代码,以及在提供的数据库中手动创建的所有服务器操作和计算字段的代码。
需要使用 --addons-path 选项来指定模块文件夹的路径。
如果与 --path 结合使用,则计数将是两个选项结果的总和(可能有重叠)。这两个选项中至少需要一个来指定要处理的代码。
$ odoo-bin cloc --addons-path=addons -d my_database
-p <path>, --path <path>
处理所提供路径中的文件。
如果与 --database 结合使用,则计数将是这两个选项结果的总和(可能有重叠)。这两个选项中至少需要一个来指定要处理的代码。
$ odoo-bin cloc -p addons/account

可以通过重复选项来提供多个路径。

$ odoo-bin cloc -p addons/account -p addons/sale

另请参阅

--addons-path <directories>
以逗号分隔的目录列表,其中存储了模块。这些目录将被扫描以查找模块。
如果使用 --database 选项,则必填。
-c <directories>

指定一个配置文件来替代 --addons-path 选项。

$ odoo-bin cloc -c config.conf -d my_database
-v, --verbose

显示每个文件的计数行的详细信息。

已处理的文件

使用 --database 选项

Odoo Cloc 统计给定数据库中额外安装模块中每个文件的行数。此外,它还计算在数据库中直接创建或导入的服务器操作和自定义计算字段的 Python 行。最后,它还计算从导入模块中的 Javascript、CSS 和 SCSS 文件以及 QWeb 视图的代码行数。

默认情况下,某些文件被排除在计数之外:

  • 清单文件 (__manifest__.py__openerp__.py)

  • 文件夹 static/lib 的内容

  • 在文件夹 testsstatic/tests 中定义的测试

  • The migrations scripts defined in the folder migrations and upgrades

  • 在清单文件的 demodemo_xml 部分声明的XML文件

对于特殊情况,可以针对每个模块定义应该被 Odoo Cloc 忽略的文件列表。这是在清单的 cloc_exclude 条目中指定的:

"cloc_exclude": [
    "lib/common.py", # exclude a single file
    "data/*.xml",    # exclude all XML files in a specific folder
    "example/**/*",  # exclude all files in a folder hierarchy recursively
    "**/*.scss",     # exclude all scss file from the module
]
可以使用模式 **/* 来忽略整个模块。这对于排除模块的维护服务费用是有用的。
有关模式语法的更多信息,请参阅 glob

使用 --path 选项

此方法与 –database option 相同,如果给定文件夹中存在清单文件,则与之一样。否则,它会计算所有文件。

识别额外模块

为了区分标准模块和额外模块,Odoo Cloc 使用以下启发式方法:位于与 basewebweb_enterprise 标准模块相同的父目录中(在跟随符号链接后的真实文件系统路径)的模块被视为标准模块。其他模块被视为额外模块。

错误处理

Odoo Cloc 无法计算某些文件。这些文件将在输出的末尾报告。

超过最大文件大小限制

Odoo Cloc 拒绝任何大于 25MB 的文件。通常,源文件小于 1 MB。如果文件被拒绝,可能是以下原因:

  • 一个包含大量数据的生成的 XML 文件。它应该在清单中排除。

  • 应该放置在 static/lib 文件夹中的 JavaScript 库。

语法错误

Odoo Cloc 无法计算具有语法问题的 Python 文件的代码行数。如果额外的模块包含这样的文件,应该修复它们以允许模块加载。如果模块在存在这些文件的情况下仍然工作,那么它们可能没有被加载,因此应该从模块中删除,或者至少通过 cloc_exclude 在清单中排除。

TSConfig生成器

在编写JavaScript时,有一些方法可以帮助您的编辑器提供强大的自动完成功能。其中一种方法是使用tsconfig.json文件。虽然最初是为TypeScript设计的,但编辑器也可以使用它的信息来处理纯JavaScript。使用此配置文件,您现在可以在模块之间获得完整的自动完成功能。

生成此文件的命令需要尽可能多的未命名参数。这些是您的插件目录的相对路径。在下面的示例中,我们向上移动一个文件夹,将tsconfig文件保存在包含社区和企业的文件夹中。

$ community/odoo-bin tsconfig --addons-path community/addons,community/odoo/addons,enterprise > tsconfig.json