Skip to content

Commit 2b74294

Browse files
committed
support null values for optional fields
Signed-off-by: wiseaidev <[email protected]>
1 parent ac6a75b commit 2b74294

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

aredis_om/model/model.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ async def get(cls, pk: Any) -> "HashModel":
13161316
if not document:
13171317
raise NotFoundError
13181318
try:
1319+
# restore none values
1320+
document = {key: val if val != "0" else None for key, val in document.items()}
13191321
result = cls.parse_obj(document)
13201322
except TypeError as e:
13211323
log.warning(
@@ -1332,14 +1334,14 @@ async def get(cls, pk: Any) -> "HashModel":
13321334
@no_type_check
13331335
def _get_value(cls, *args, **kwargs) -> Any:
13341336
"""
1335-
Always send None as an empty string.
1337+
Always send None as a zero string: "0" to handle Optional int and float fields.
13361338
13371339
TODO: We do this because redis-py's hset() method requires non-null
13381340
values. Is there a better way?
13391341
"""
13401342
val = super()._get_value(*args, **kwargs)
13411343
if val is None:
1342-
return ""
1344+
return "0"
13431345
return val
13441346

13451347
@classmethod

tests/test_hash_model.py

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Member(BaseHashModel):
4848
last_name: str = Field(index=True)
4949
email: str = Field(index=True)
5050
join_date: datetime.date
51+
height: Optional[int] = None
52+
weight: Optional[float] = None
5153
age: int = Field(index=True, sortable=True)
5254
bio: str = Field(index=True, full_text_search=True)
5355

0 commit comments

Comments
 (0)