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:
JavaScript
x
5
1
class Item4(BaseModel):
2
balance: Optional[float] = None
3
currencyCode: Optional[str] = None
4
customerId: Optional[int] =None
5
My function:
JavaScript
1
5
1
@app.put("/updateBalance/{balanceId}")
2
async def update_item(item: Item4, balanceId):
3
msg = r.hmset(balanceId, dict(item))
4
return msg
5
Advertisement
Answer
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:
JavaScript
1
2
1
r.hmset(balanceId, item.dict(exclude_unset=True))
2