TableNameGetter
ClassDescriptorSource
class AnyClass(sa.orm.DeclarativeBase):
__tablename__ = TableNameGetter()
val: sa.orm.Query[AnyClass] = AnyClass.query
The getter of the __tablename__
property.
Use this descriptor like this:
class Base(sa.orm.DeclarativeBase):
__tablename__ = TableNameGetter()
class NewModel(Base): ...
or like this:
class NewModel(Base):
__tablename__ = TableNameGetter()
It will
- Prevent users to modify
__tablename__
of an instance of the model class. - Return an automatically synthesized table name if it is not defined.
tip
You may not need to use this descriptor, because the following codes will have the
equivalent effect. The following usage is also recommended by
sqlalchemy
:
class AnyClass(sa.orm.DeclarativeBase):
@declared_attr.directive
def __tablename__(cls) -> str:
return fsc.utilities.TableNameGetter.calc_auto_table_name(cls.__name__)
Aliases
This class can be acquired by
import flask_sqlalchemy_compat as fsc
fsc.TableNameGetter
fsc.utilities.TableNameGetter