redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.06k stars 108 forks source link

Private field with underscore prefix #604

Closed zidokobik closed 2 months ago

zidokobik commented 2 months ago

Currently, any optional field name that starts with _ is not stored in the database. Which makes implementing the getter/setter pattern a nuisance.

class User(JsonModel):
  public_attribute: str = Field(index=True)  # will be stored
  _private_attribute : typing.Optional[str] = Field(index=True)  # will NOT be stored

Is there any workaround for this implementation?

webdz9r commented 2 months ago

seems this is a Pydantic thing via the private model attributes

slorello89 commented 2 months ago

IMO this is the correct behavior, so no I don't think there is a workaround (nor would one be appropriate).

This goes back to OOP, if you declare a field as a private you are saying you only want the class itself to be responsible for mutating the field. There are conventions in other languages (e.g. Java) to allow setters and getters when you want things like serializers to have access to the field, but this is python so the most correct way to manage this would be for the field to not be private.