【发布时间】:2022-01-22 03:18:54
【问题描述】:
我有课
class Employee(BaseModel):
Emp_ID: int
Emp_Name: str
Emp_Qualification: str
下面是 POST 请求的代码,我在最后一次返回时遇到错误,我试图获取employee["Emp_id"] 值。
@app.api_route("/post-details/{postid}", methods=["POST"])
def post(postid: int , employee : Employee):
newlist = []
cursor=conn.cursor()
getemploy2= "select id from employee3"
cursor.execute(getemploy2)
result = cursor.fetchall()
for list in result:
newlist.extend(list)
if postid in newlist:
return "yes it exist"
return employee(["Emp_ID"])
【问题讨论】:
-
要获取对象的属性,既不能使用
(),也不能使用[],也不能使用""。你的意思是return employee.Emp_ID? -
当访问一个实例的属性时,你必须这样做:
employee.Emp_ID,而不是方括号和圆括号。我建议你先学习更多关于 Python 的 OOP :) -
很棒的 mkrieger,谢谢