redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.12k stars 112 forks source link

Allows writing but not reading, on model with optional field. #254

Open vbabiy opened 2 years ago

vbabiy commented 2 years ago
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.

linyaoli commented 2 years ago

+1 on this, any solution yet? 🙏

LilianBoulard commented 2 years ago

+1 This is essentially the same issue as https://github.com/redis/redis-om-python/issues/38

sav-norem commented 2 years ago

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

vbabiy commented 1 year ago

@sav-norem we are hosted on GCP and memorystore doesn't have support for JSON yet.

Phailin791 commented 1 year ago

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