Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
vbabiy opened this issue May 16, 2022 · 5 comments
Open

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

vbabiy opened this issue May 16, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@vbabiy
Copy link

vbabiy commented May 16, 2022

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.

@linyao-reevo
Copy link

+1 on this, any solution yet? 🙏

@LilianBoulard
Copy link

LilianBoulard commented Jun 17, 2022

+1
This is essentially the same issue as #38

@sav-norem
Copy link

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
Copy link
Author

vbabiy commented Nov 2, 2022

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

@Phailin791
Copy link

Phailin791 commented Sep 26, 2023

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

@abrookins abrookins added the bug Something isn't working label Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants