gino.loader module¶
-
class
gino.loader.AliasLoader(alias, *columns, **extras)¶ Bases:
gino.loader.ModelLoaderThe same as
ModelLoader, kept for compatibility.
-
class
gino.loader.CallableLoader(func)¶ Bases:
gino.loader.LoaderLoad the row by calling a specified function.
- Parameters
func – A
callable()taking 2 positional arguments(row, context)that will be called indo_load(), returning the loaded result.
-
class
gino.loader.ColumnLoader(column)¶ Bases:
gino.loader.LoaderLoad a given column in the row.
-
do_load(row, context)¶ Interface used by GINO to run the loader.
- Parameters
row – A
RowProxyinstance.context – Not used.
- Returns
The value of the specified column, followed by
True.
-
-
class
gino.loader.Loader¶ Bases:
objectThe abstract base class of loaders.
Loaders are used to load raw database rows into expected results.
GinoEnginewill use the loader set on theloadervalue of theexecution_options(), for example:from sqlalchemy import text, create_engine from gino.loader import ColumnLoader e = await create_engine("postgresql://localhost/gino", strategy="gino") q = text("SELECT now() as ts") loader = ColumnLoader("ts") ts = await e.first(q.execution_options(loader=loader)) # datetime
-
do_load(row, context)¶ Interface used by GINO to run the loader.
Must be implemented in subclasses.
- Parameters
row – A
RowProxyinstance.context – A
dictthat is reused across all loaders in one query.
- Returns
Any result that the loader is supposed to return, followed by a boolean value indicating if the result is distinct.
-
classmethod
get(value)¶ Automatically create a loader based on the type of the given value.
value type
loader type
as is
any other types
- Parameters
value – Any supported value above.
- Returns
A loader instance.
-
get_columns()¶ Generate a list of selectables from this loader.
This is an experimental feature, this method is supposed to be called by
query.- Returns
A
listof SQLAlchemy selectables.
-
get_from()¶ Generate a clause to be used in
select_from()from this loader.This is an experimental feature, this method is supposed to be called by
query.- Returns
A
FromClauseinstance, orNone.
-
property
query¶ Generate a query from this loader.
This is an experimental feature, not all loaders support this.
- Returns
A query instance with the
loaderexecution option set to self.
-
-
class
gino.loader.ModelLoader(model, *columns, **extras)¶ Bases:
gino.loader.LoaderA loader that loads a row into a GINO model instance.
This loader generates an instance of the given
modeltype and fills the instance with attributes according to the other given parameters:Load each column attribute listed in the given
columnspositional arguments.If
columnsis not given, all defined columns of themodelwill be loaded.For each keyword argument, its value will be used to generate a loader using
Loader.get(), and the loaded value will besetattr()to the model instance under the name of the key.
For example, the simplest select and load:
sqlalchemy.select([User]).execution_options(loader=ModelLoader(User))
Select only the name column, and still load it into model instances:
sqlalchemy.select( [User.name] ).execution_options( loader=ModelLoader(User, User.name) )
This would also yield
Userinstances, with all column attributes asNonebutname.Nest a
ValueLoader:sqlalchemy.select( [User.name] ).execution_options( loader=ModelLoader(User, id=1) )
1is then converted into aValueLoaderand mocked theidattribute of all returnedUserinstances as1.Nest another
ModelLoader:sqlalchemy.select( [user.outerjoin(Company)] ).execution_options( loader=ModelLoader(User, company=Company) )
Likewise,
Companyis converted into aModelLoaderto load theCompanycolumns from the joined result, and theCompanyinstances are set to thecompanyattribute of eachUserinstance usingsetattr().- Parameters
-
distinct(*columns)¶ Configure this loader to reuse instances that have the same values of all the give columns.
- Parameters
columns – Preferably
Columninstances.- Returns
selffor chaining.
-
do_load(row, context)¶ Interface used by GINO to run the loader.
- Parameters
row – A
RowProxyinstance.context – A
dictthat is reused across all loaders in one query.
- Returns
The model instance, followed by a boolean value indicating if the result is distinct.
-
get_columns()¶ Generate a list of selectables from this loader.
This is an experimental feature, this method is supposed to be called by
query.- Returns
A
listof SQLAlchemy selectables.
-
get_from()¶ Generate a clause to be used in
select_from()from this loader.This is an experimental feature, this method is supposed to be called by
query.- Returns
A
FromClauseinstance, orNone.
-
load(*columns, **extras)¶ Update the loader with new rules.
After initialization, the rules of this loader can still be updated. This is useful when using the model class as a shortcut of
ModelLoaderwhere possible, chaining with aload()to initialize the rules, for example:sqlalchemy.select( [user.outerjoin(Company)] ).execution_options( loader=ModelLoader(User, company=Company.load('name')) )
- Parameters
columns – If provided, replace the columns to load with the given ones.
extras – Update the loader with new extras.
- Returns
selffor chaining.
-
none_as_none(enabled=True)¶ Deprecated method for compatibility, does nothing.
-
on(on_clause)¶ Specify the
on_clausefor generating joined queries.This is an experimental feature, used by
get_from().- Parameters
on_clause – An expression to feed into
join().- Returns
selffor chaining.
-
class
gino.loader.TupleLoader(values)¶ Bases:
gino.loader.LoaderLoad multiple values into a tuple.
- Parameters
values – A
tuple, each item is converted into a loader withLoader.get().
-
class
gino.loader.ValueLoader(value)¶ Bases:
gino.loader.LoaderA loader that always return the specified value.
- Parameters
value – The value to return on load.
-
do_load(row, context)¶ Interface used by GINO to run the loader.
- Parameters
row – Not used.
context – Not used.
- Returns
The given value, followed by
True.