如何覆盖 Flask 应用程序的视图功能

问题描述 投票:0回答:1

我正在使用 Flask(0.11.1) 和 OpenERP,并且:

  1. 我已经定义了一个 url 规则:
@app.route("/sync_order")
def sync_order(): 
   print(1)
  1. 我想在其他OE模块中覆盖它:
@app.route("/sync_order")
def sync_order(): 
   print(1)

# override it

@app.route("/sync_order")
def sync_order2(): 
   print(2)
  1. 但是我得到了一个错误:
AssertionError: View function mapping is overwriting an existing endpoint function: sync_order
  1. 这是我当前的解决方案:
@app.route("/sync_order")
def sync_order(): 
   print(1)

def sync_order2(): 
   print(2)

# change the mapping of endpoint and view functions
# print `2` now
with app.app_context() as context:
    context.app.view_functions['sync_order'] = sync_order2

这是我的问题:

  1. 这样做可以吗,或者我有潜在的问题吗?
  2. 还有其他方法可以覆盖视图函数吗?
  3. 不要更改原始函数,因为如果我没有该模块,该函数应该打印

谢谢

python flask odoo
1个回答
0
投票

如果有效,我想它会有效

¯\_(ツ)_/¯

© www.soinside.com 2019 - 2024. All rights reserved.