odoo dbfilter,db_name 的配置,指定多个数据库

2021/8/9 19:06:26

本文主要是介绍odoo dbfilter,db_name 的配置,指定多个数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

个人偏向配置dbfilter,

  1. db_name指定多个数据库,可以使用逗号隔开,如db_name=db1,db2,但是db1,和db2,必须实际存在数据库中,不然启动报错,这就是我不喜欢使用的原因
  2. dbfilter使用正则表达式的方式可以指定多个数据库:dbfilter=db1|db2

补充:

dbfilter 有时候无法获取所有的数据库名:后来查到的原因是使用不同的数据库账号创建的数据库不能交叉使用,即使是superuser;
获取所有数据库的代码,注意sql部分中有对datdba做筛选

def list_dbs(force=False):
    if not odoo.tools.config['list_db'] and not force:
        raise odoo.exceptions.AccessDenied()

    if not odoo.tools.config['dbfilter'] and odoo.tools.config['db_name']:
        # In case --db-filter is not provided and --database is passed, Odoo will not
        # fetch the list of databases available on the postgres server and instead will
        # use the value of --database as comma seperated list of exposed databases.
        res = sorted(db.strip() for db in odoo.tools.config['db_name'].split(','))
        return res

    chosen_template = odoo.tools.config['db_template']
    templates_list = tuple(set(['postgres', chosen_template]))
    db = odoo.sql_db.db_connect('postgres')
    with closing(db.cursor()) as cr:
        try:
            cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename=current_user) and not datistemplate and datallowconn and datname not in %s order by datname", (templates_list,))
            res = [odoo.tools.ustr(name) for (name,) in cr.fetchall()]
        except Exception:
            _logger.exception('Listing databases failed:')
            res = []
    return res


这篇关于odoo dbfilter,db_name 的配置,指定多个数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程