Fastapi:put方法

发布于 2025-01-18 05:38:41 字数 959 浏览 0 评论 0原文

我希望能够在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2025-01-25 05:38:41

我不确定你到底想要什么,但如果你想部分更新数据,也许这可能会帮助你 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文