Skip to content
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

Private field with underscore prefix #604

Closed
zidokobik opened this issue Apr 7, 2024 · 2 comments
Closed

Private field with underscore prefix #604

zidokobik opened this issue Apr 7, 2024 · 2 comments

Comments

@zidokobik
Copy link

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

webdz9r commented Apr 9, 2024

seems this is a Pydantic thing via the private model attributes

@slorello89
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants