访问Flask URL转换器中的其他路径参数

时间:2017-01-20 01:49:39

标签: python flask werkzeug

我想为Flask应用创建一些自定义网址转换器。资源映射到ORM模型。我希望能够访问以前的URL路径参数来确定转换时的关系。这样的事情可能吗?

class CustomObject1Converter(BaseConverter):
    def to_python(self, value):
        obj = Obj1.get_by(id=value)
        return obj

class CustomObject2Converter(BaseConverter):
    def to_python(self, value):
        # somehow get access to object1 path parameter here
        obj2 = Obj2.query.filter(Obj2.obj1 == obj1).filter(obj2.id == value).first()
        return obj2

app.url_map.converters['object1'] = CustomObject1Converter
app.url_map.converters['object2'] = CustomObject2Converter

app.route('/objects1/<object1:object1>/objects2/<object2:object2>')
def get_object_2(object1, object2):
    # do some things and return some data
    ...

0 个答案:

没有答案