0

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)}
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
znb
  • 33
  • 1
  • 8
  • You'll have to define the input parameter in some way - if your input schema doesn't match, don't use it. What value do you want to change in your `update_data` function? BTW: do not use `np.array` in that way - use `SELECT id FROM users WHERE id = ..`, syntax instead (with placeholders) to only select the actual user. There is no need to fetch all users, convert it to an np array and then check if the id is in the array. – MatsLindh Mar 31 '22 at 13:56

1 Answers1

1

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/

asqa
  • 11
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '22 at 04:36