接受Flask url中的int列表而不是一个int

时间:2015-08-26 22:11:18

标签: python flask

我的API有一个通过url中传递的int id处理用户的路由。我想传递一个id列表,这样我就可以向API发出一个批量请求,而不是几个单个请求。我如何接受ID列表?

@app.route('/user/<int:user_id>')  # should accept multiple ints
def process_user(user_id):
    return str(user_id)

2 个答案:

答案 0 :(得分:3)

写一个custom url converter,它接受​​由分隔符分隔的整数列表,而不是一个整数。例如,Stack Exchange API接受由分号分隔的多个ID: SET NOCOUNT ON; SELECT rf.Raving_Fan_ID, rf.Raving_Fan_Name AS "Raving Fan", cr.Contact_Role_Name AS "Contact Type", emp.Emp_Name AS "TCS Employee", rf.FU_Detail AS "Follow Ups", CASE WHEN rf.FU_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.FU_Date ELSE NULL END AS "FU Date", CASE WHEN rf.FU_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.FU_Point ELSE NULL END AS "FU Point", rf.Test_Detail AS "Testimonials", CASE WHEN rf.Test_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Test_Date ELSE NULL END AS "Test Date", CASE WHEN rf.Test_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Test_Point ELSE NULL END AS "Test Point", rf.Ref_Detail AS "Referrals", CASE WHEN rf.Ref_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Ref_Date ELSE NULL END AS "Ref Date", CASE WHEN rf.Ref_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Ref_Point ELSE NULL END AS "Ref Point", CASE WHEN rf.Signed_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Signed_Date ELSE NULL END AS "Signed Date", CASE WHEN rf.Signed_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Signed_Point ELSE NULL END AS "Signed Point", CASE WHEN rf.Bonus_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Bonus_Date ELSE NULL END AS "Bonus Date", CASE WHEN rf.Bonus_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Bonus_Point ELSE NULL END AS "Bonus Point", (CASE WHEN rf.Ref_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Ref_Point ELSE 0 END + CASE WHEN rf.Signed_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Signed_Point ELSE 0 END + CASE WHEN rf.FU_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.FU_Point ELSE 0 END + CASE WHEN rf.Test_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Test_Point ELSE 0 END + CASE WHEN rf.Bonus_Date BETWEEN CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE()) THEN rf.Bonus_Point ELSE 0 END) AS "Total Points" FROM [dbo].[Raving_Fans] AS rf LEFT JOIN Employees AS emp ON rf.Emp_ID = emp.Emp_ID LEFT JOIN Contact_Role AS cr ON rf.Contact_Role_ID = cr.Contact_Role_ID WHERE (rf.FU_Date between CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE())) OR (rf.Test_Date between CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE())) OR (rf.Ref_Date between CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE())) OR (rf.Signed_Date between CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE())) OR (rf.Bonus_Date between CONVERT(date,DATEADD(month, -6, GETDATE())) and CONVERT(date, GETDATE())) AND(emp.Emp_Name LIKE '%' +@Emp_Name+ '%' OR @Emp_Name IS NULL) ORDER BY emp.Emp_Name ASC; 。将转换器注册到您的应用程序并在您的路线中使用它。

/answers/1;2;3

答案 1 :(得分:1)

就这么简单

@app.route('/user/<users>')
def process_user(users):
    users = list(users.split(","))
    print(users)

输入

/users/[1,2,3,4]

输出

[1,2,3,4]