Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,8 @@ def to_string(s):
map(to_string, res[i + offset][1::2]),
)
)
# restore null values
fields = {key: val if val not in ["0", "0.0"] else None for key, val in fields.items()}
# $ means a json entry
if fields.get("$"):
json_fields = json.loads(fields.pop("$"))
Expand Down Expand Up @@ -1344,6 +1346,8 @@ async def get(cls, pk: Any) -> "HashModel":
if not document:
raise NotFoundError
try:
# restore none values
document = {key: val if val not in ["0", "0.0"] else None for key, val in document.items()}
result = cls.parse_obj(document)
except TypeError as e:
log.warning(
Expand All @@ -1360,14 +1364,14 @@ async def get(cls, pk: Any) -> "HashModel":
@no_type_check
def _get_value(cls, *args, **kwargs) -> Any:
"""
Always send None as an empty string.
Always send None as a zero string: "0" to handle Optional int and float fields.

TODO: We do this because redis-py's hset() method requires non-null
values. Is there a better way?
"""
val = super()._get_value(*args, **kwargs)
if val is None:
return ""
return "0"
return val

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Member(BaseHashModel):
join_date: datetime.date
age: int = Field(index=True, sortable=True)
bio: str = Field(index=True, full_text_search=True)
height: Optional[int] = Field(index=True)
weight: Optional[float] = Field(index=True)

class Meta:
model_key_prefix = "member"
Expand Down