如何从同一flask-restplus资源类获取和发布端点?

时间:2018-12-25 06:12:50

标签: python flask flask-restplus

我有下面的类继承自flask-restplus.Resource。

class Patient(Resource):
    """ Patient endpoint."""
    @clinic_api_ns.route("/patient/add/")
    def post(self):
        # TODO: add a patient.
        return {}

    @clinc_api_ns.route("/patient/<string:name>")
    def get(self, name):
        # TODO: get a patient record
        return {}   

我想从以上实现2个端点,但是不起作用 它会引发错误:

  

/site-packages/flask_restplus/api.py”,第287行,在_register_view中       resource_func = self.output(resource.as_view(endpoint,self,* resource_class_args,AttributeError:'function'对象没有属性'as_view'

2 个答案:

答案 0 :(得分:1)

您需要在类上使用装饰器,而不要使用函数。

您API将击中相同的端点“/患者”,并且该方法将确定哪个函数被调用。获取,发布,放置和删除。

如果您需要不同的API端点则需要2个资源类,一个用于每个路径。

答案 1 :(得分:0)

据我了解,.route() decorators仅可用于Resource类,而不能用于它们的方法。您必须定义两个单独的资源-与in the full example in the docsTodoTodoList的定义类似。

相关问题