redis-om-python
redis-om-python copied to clipboard
Allows writing but not reading, on model with optional field.
from typing import Optional
from redis_om import HashModel
class TestObject(HashModel):
name: str
age: int
weight: Optional[float] = None
obj = TestObject(name="Joe", age=20, weight=None)
obj.save()
TestObject.get(obj.pk)
# pydantic.error_wrappers.ValidationError: 1 validation error for TestObject
# weight
# value is not a valid float (type=type_error.float)
If was able to write it I should be able to read it.
+1 on this, any solution yet? 🙏
+1 This is essentially the same issue as https://github.com/redis/redis-om-python/issues/38
This is being worked on - but in the meantime if you can, this issue only applies to HashModels and you can swap it for a JsonModel and it will work
@sav-norem we are hosted on GCP and memorystore doesn't have support for JSON yet.
on my way, i do it like this:
class BaseModel(HashModel, ABC): class Meta: global_key_prefix = "customer-dashboard" database = redis
@classmethod
def parse_obj(cls, obj: Any):
for name, field in cls.__fields__.items():
if not field.required and obj[name]=="":
obj[name] = None
return super().parse_obj(obj)
class TestObject(BaseModel): name: str age: int weight: Optional[float] = None