猿问

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

我有以下继承自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", line 287, in _register_view resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args, AttributeError: 'function' object has no attribute 'as_view'


开心每一天1111
浏览 285回答 2
2回答

温温酱

您需要在类中使用装饰器而不是函数。您的 API 将访问相同的端点“/patient”,该方法将确定调用哪个函数。获取、发布、放置和删除。如果您需要不同的 API 端点,您将需要 2 个资源类,每个路径一个。

哆啦的时光机

据我所知,.route()装饰器只能用于Resource类 - 而不是它们的方法。您必须定义两个单独的资源 - 类似于文档中完整示例中定义的方式Todo和TodoList方式。
随时随地看视频慕课网APP

相关分类

Python
我要回答