Closed masreplay closed 1 year ago
@masreplay any updates during the year? 🤔 Did you find a solution?
Yes i found one, i stopped using fastapi and using nest instead
Maybe it will be useful for someone who is passing through the issue. I've made a very rude adoption of hybrid_property
i18n approach through the SQLModel Field
:
class SaTranslationFieldComparator(JSONB.Comparator):
def __eq__(self, other: Any) -> bool:
return self.contains({"en": other})
class SaTranslationField(types.TypeDecorator):
impl: JSONB = JSONB()
cache_ok = True
comparator_factory = SaTranslationFieldComparator
def coerce_compared_value(self, op: Any, value: Any) -> types.TypeEngine[Any]:
return self.impl.coerce_compared_value(op, value)
def process_bind_param(self, value: Any, dialect: Any) -> Optional[dict]: # type: ignore
if value is None:
return None
return {"en": value}
def process_result_value(self, value: Any, dialect: Any) -> Optional[Any]:
translated = value.get("en")
return translated
def TranslationField(**kwargs: Any) -> Any:
return Field(sa_column=Column(SaTranslationField), nullable=False, default=dict(), **kwargs)
Usage:
name: str = TranslationField()
In your alembic create an appropriate Column in the Database:
sa.Column("name", SaTranslationField, nullable=False, server_default="{}"),
First Check
Commit to Help
Example Code
Description
translate key in the database
Wanted Solution
I want to translate filed in data base using store pg extension or any other way if it's possibledatabase
Wanted Code
Alternatives
No response
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.7
Additional Context
No response