redis-om-python icon indicating copy to clipboard operation
redis-om-python copied to clipboard

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

Open vbabiy opened this issue 3 years ago • 5 comments
trafficstars

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.

vbabiy avatar May 16 '22 14:05 vbabiy

+1 on this, any solution yet? 🙏

linyao-reevo avatar May 23 '22 10:05 linyao-reevo

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

LilianBoulard avatar Jun 17 '22 12:06 LilianBoulard

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 avatar Sep 09 '22 16:09 sav-norem

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

vbabiy avatar Nov 02 '22 11:11 vbabiy

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

Phailin791 avatar Sep 26 '23 07:09 Phailin791