我有以下繼承自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'
2 回答

溫溫醬
TA貢獻1752條經驗 獲得超4個贊
您需要在類中使用裝飾器而不是函數。
您的 API 將訪問相同的端點“/patient”,該方法將確定調用哪個函數。獲取、發布、放置和刪除。
如果您需要不同的 API 端點,您將需要 2 個資源類,每個路徑一個。

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
據我所知,.route()
裝飾器只能用于Resource
類 - 而不是它們的方法。您必須定義兩個單獨的資源 - 類似于文檔中完整示例中定義的方式Todo
和TodoList
方式。
添加回答
舉報
0/150
提交
取消