I'm using Python FastAPI with redis. I wrote a function to update values in a redis hash, but I couldn't able to update a single value alone, I could only able to re-write the whole hash.
My model:
class Item4(BaseModel):
balance: Optional[float] = None
currencyCode: Optional[str] = None
customerId: Optional[int] =None
My function:
@app.put("/updateBalance/{balanceId}")
async def update_item(item: Item4, balanceId):
msg = r.hmset(balanceId, dict(item))
return msg
hmset
is deprecated for hset
- but if you want to only update a single key, do not send an hash with all the keys present.
You can use the exclude_unset
parameter to Pydantic's dict()
method to not include any values that hasn't been explicitly provided:
r.hmset(balanceId, item.dict(exclude_unset=True))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.