Fastapi:put方法
我希望能够在Swagger中仅更改POT方法中的一个字段,而不必为PUT定义一个新类。在这种情况下,由于所有字段都是必需的在看台字段中,但我只想在不添加新类的情况下更改一个字段。 感谢您的帮助
from pydantic import BaseModel
class User(BaseModel):
name: str
email: str
password: str
#------------------------------------------------------
from fastapi import FastAPI
user_ = FastAPI()
@user_.post("/")
def write_data(user_: User):
conn.execute(users.insert().values(
name= user_.name,
email= user_.email,
password= user_.password
))
return{ 'New data added' }
@user_.put("/{id}")
def update_data(id: int, user_: User):
a=conn.execute("SELECT id FROM users")
b=a.fetchall()
my_list=np.array(b)
if id in my_list:
conn.execute(users.update().values(
name= user_.name,
email= user_.email,
password= user_.password
).where(users.c.id == id ))
return {'data updated white ID ' +str(id)}
else:
return {'No data has been registered for ID ' +str(id)}
I want to be able to change only one field in the put method at the swagger without having to define a new class for put.In this case, since all fields are required, I do not have permission to delete them and change only one item in the put field, but I just want to change one field without adding a new class.
Thank you for your help
from pydantic import BaseModel
class User(BaseModel):
name: str
email: str
password: str
#------------------------------------------------------
from fastapi import FastAPI
user_ = FastAPI()
@user_.post("/")
def write_data(user_: User):
conn.execute(users.insert().values(
name= user_.name,
email= user_.email,
password= user_.password
))
return{ 'New data added' }
@user_.put("/{id}")
def update_data(id: int, user_: User):
a=conn.execute("SELECT id FROM users")
b=a.fetchall()
my_list=np.array(b)
if id in my_list:
conn.execute(users.update().values(
name= user_.name,
email= user_.email,
password= user_.password
).where(users.c.id == id ))
return {'data updated white ID ' +str(id)}
else:
return {'No data has been registered for ID ' +str(id)}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你到底想要什么,但如果你想部分更新数据,也许这可能会帮助你 https://fastapi.tiangolo.com/tutorial/body-updates/
I'm not sure what actually you want but if you want to partially update data maybe this might help you https://fastapi.tiangolo.com/tutorial/body-updates/