[英]update object in database with fastapi
i am new to fastapi, and i want to be able to update my bank account information when a deposit is made.我是 fastapi 的新手,我希望能够在存款时更新我的银行账户信息。 i am not sure how to do this as i am not sure how patch works.我不确定如何执行此操作,因为我不确定补丁是如何工作的。 the logic used in deposit is that i used in create a bank account (obviously they should not work the same).存款中使用的逻辑是我在创建银行账户时使用的(显然它们不应该工作相同)。 this is my model:这是我的 model:
class Account(Base):
__tablename__ = "accounts"
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
deposits_made = Column(Integer)
total = Column(Integer)
class PaymentRequest(BaseModel):
bank_id: str
amount: int
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@app.patch("/deposit")
def deposit(request: PaymentRequest, db: Session = Depends(get_db)):
account = db.query(Account)
account = accounts[request.id]
account.total += request.amount
account.deposits_made += 1
db.add(account)
db.commit()
return{
"code":"success",
"message":"donation made"
}
Your table needs to be specific您的表格需要具体
class Account(Base):
__tablename__ = "accounts"
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
deposits_made = Column(Integer)
total = Column(Integer)
Your schema should be a good reflection of your table but if you like to go with what you have:您的架构应该很好地反映您的表格,但如果您喜欢 go 与您所拥有的:
class PaymentRequest(BaseModel):
bank_id: str
amount: int
then some calculations have to be made to be able to have a successful table update because what must go to the table is:然后必须进行一些计算才能成功更新表,因为表中的 go 必须是:
{'name':name, 'deposits_made':deposits_made, 'total ':total}
remember, your incoming data is just:请记住,您的传入数据只是:
bank_id: str
amount: int
and your table does not have provision made for such, that being said,而你的餐桌上没有这样的规定,话虽如此,
if bank_id
is the same as the name on the Account table you should have given it a better name at least
depositor_name , unique, ...如果bank_id
与 Account 表上的名称相同, you should have given it a better name at least
depositor_name ,唯一的,...
when you have decided to make changes to your table, the code below will help:当您决定对表格进行更改时,下面的代码将有所帮助:
@app.put("/deposit/{id}")
def deposit(id, request:schemas.PaymentRequest, db:Session=Depends(get_db)):
old_account = db.query(models.Account).filter(models.Account.id == id)
if not old_account.first():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f'old_account with the id {id} is not available')
old_account.update({'name':name, 'deposits_made':deposits_made, 'total ':total})
db.commit()
return {"code":"success","message":"donation made"}
The idea of a PUT
request is to update the data of a certain object. PUT
请求的思想是更新某个object的数据。
First things first, for REST APIs
, it is typical to reference the object in the url, e.. via an id.首先,对于REST APIs
,通常通过 id 引用 url 中的 object。 Your url would become something like this domain.com/path/to/resource/id
你的 url 会变成这样domain.com/path/to/resource/id
https://www.edureka.co/blog/what-is-rest-api/ https://www.edureka.co/blog/what-is-rest-api/
This being said, your code will now look as follows话虽如此,您的代码现在如下所示
@app.patch("/deposit/{id}")
def deposit(request: PaymentRequest, db: Session = Depends(get_db)):
# Not sure what kind of orm you are using, but you get the idea
account = db.query(Account.id=id)
account = accounts[request.id]
account.total += request.amount
account.deposits_made += 1
db.add(account)
db.commit()
return{
"code":"success",
"message":"donation made"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.