odoo的访问域名–不用进入数据库选择页面 配置参数dbfilter
我们每次登陆的时候,都会进入/web/database/select页面
如何避免不用选择,而是直接进入指定的数据库呢?
dbfilter
当我们需要根据域名来匹配数据库时(比如saas环境)这样就不适用了,这个时候就可以用 dbfilter 这个配置项来实现
dbfilter 默认值为 .*
eg: dbfilter = ^%h$ 表示按域名精确匹配数据库服务器中名称为域名的数据库
启动参数 --db-filter=’^%d$’ 表示按二级域名前缀精确匹配对应名称的数据库(注意:127.0.0.1访问时会被匹配为 127 库名)
可用的匹配替代符号有 %h 和 %d
%h
%h 代表访问访问的域名,比如 www.abc.com
%d
当访问地址为 www.abc.com 时 %d 为 abc
源码分析
def db_filter(dbs, httprequest=None):
httprequest = httprequest or request.httprequest
h = httprequest.environ.get('HTTP_HOST', '').split(':')[0]
d, _, r = h.partition('.')
if d == "www" and r:
d = r.partition('.')[0]
if odoo.tools.config['dbfilter']:
r = odoo.tools.config['dbfilter'].replace('%h', h).replace('%d', d)
dbs = [i for i in dbs if re.match(r, i)]
elif odoo.tools.config['db_name']:
# In case --db-filter is not provided and --database is passed, Odoo will
# use the value of --database as a comma seperated list of exposed databases.
exposed_dbs = set(db.strip() for db in odoo.tools.config['db_name'].split(','))
dbs = sorted(exposed_dbs.intersection(dbs))
return dbs