简体   繁体   English

2到Flask-RESTful中到相同资源的GET路由

[英]2 GET routes to same Resource in Flask-RESTful

I am learning Flask-RESTful and I have the following task i want to do: 我正在学习Flask-RESTful,并且我想执行以下任务:

There are these 2 GET Routes 有这2条GET路线

GET /student/id (get student details, search student by ID) GET /学生/ ID(获取学生详细信息,按ID搜索学生)
GET /student/id/grades (get student grades, search student by ID) GET /学生/ ID /成绩(获取学生成绩,按ID搜索学生)

If i don't want to have if statement in student GET function, how can I have this implemented? 如果我不想在学生GET函数中使用if语句,该如何实现呢? I must create 2 different resources? 我必须创建2个不同的资源吗? Student and GradesList? 学生和成绩列表?

Thanks, Alon 谢谢阿隆

Change the order to 更改顺序为

/student/id/grades

/student/id

The error happens because route searching happens in the order in which you list them. 发生此错误是因为路由搜索是按照列出它们的顺序进行的。

For eg. 例如。 say you have two routes as follows: /a/b and /a/ Let's consider two cases - 假设您有两条路线,分别为: /a/b/a/让我们考虑两种情况-

Order 1 订单1

/a/ /一种/

/a/b/ / A / B /

Now if you search for /a/<some id> then it matches the first route and you are routed accordingly. 现在,如果您搜索/a/<some id>则它与第一条路线匹配,因此您将被相应地路由。 Again, when you search for /a/b/<some id> , the prefix ie /a/ matches again and you are routed to the first route. 再次,当您搜索/a/b/<some id> ,前缀/a/再次匹配,您将被路由到第一条路由。

Order 2 - 订单2-

/a/b/ / A / B /

/a/ /一种/

Now, if you search for /a/<some id> then it does not match the first route (as the prefix /a/b/ does not match). 现在,如果您搜索/a/<some id>则它与第一个路由不匹配(​​因为前缀/a/b/不匹配)。 But the second route matches and you are routed accordingly.As an alternative, if you search for /a/b/<some id> then the first route matches. 但是第二条路线匹配并且您也被相应地路由。作为替代,如果您搜索/a/b/<some id>则第一条路线会匹配。 And then you are routed to the correct URL. 然后,您将被路由到正确的URL。

As a rule of thumb, remember to put the more particular case first. 根据经验,请记住将更特殊的情况放在首位。

Yes, you should create 2 different resources as follows: 是的,您应该创建两个不同的资源,如下所示:

from flask_restful import Api

api = Api(app)

class StudentResource(Resource):

    def get(self, id):
        // Your code here. This is an example
        student = Student.get(id)

class GradeListResource(Resource):

    def get(self, id):
        // Your code here. This is an example
        student = Student.get(id)
        grades = studen.grades()

api.add_resource(StudentResource, '/student/<int:id>', endpoint='student_resource')
api.add_resource(GradeListResource, '/student/<int:id>/grades', endpoint='grade_list_resource')

Sure, You need to create 2 different resources since the second GET in the same Resource class will override the first one. 当然,您需要创建2个不同的资源,因为同一Re​​source类中的第二个GET将覆盖第一个。

However, you can still take use of simple Flask API rather than flask_restful. 但是,您仍然可以使用简单的Flask API而不是flask_restful。 You may find this thread useful: Using basic Flask vs Flask-RESTful for API development 您可能会发现此线程很有用: 使用基本Flask与Flask-RESTful进行API开发

and more importantly, this: Flask RESTful API multiple and complex endpoints 更重要的是,这是: Flask RESTful API多个复杂端点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM