Reputation: 11207
After a database is constructed in SQLAlchemy, I want to get all of the models, which is to say, the classes which represent them
>>> db
<SQLAlchemy engine='postgresql:///example'>
>>> ???
[<db.Model 'User'>, <db.Model 'Comment'>, <db.Model 'Article'>]
Can this be achieved? How?
I can get the tables decently, just not the models. Eg:
>>> for t in db.metadata.tables.items():
print(t)
this gives the tables just not the models themselves
Upvotes: 17
Views: 19159
Reputation: 4793
A simple way to do is:
import inspect
import mymodels
for name, obj in inspect.getmembers(mymodels):
if inspect.isclass(obj) and hasattr(obj, '__tablename__'):
print("model: %s %s"%(name,obj))
Upvotes: 0
Reputation: 12822
In SQLAlchemy 1.4, the _decl_class_registry.values()
method has removed, you can use db.Model.registry.mappers
instead:
models = {
mapper.class_.__name__: mapper.class_
for mapper in db.Model.registry.mappers
}
See the details in this issue.
Upvotes: 11
Reputation: 974
Here is my variation of Celeo’s answer with typing and succinct intermediary steps:
from ??? import db # Import from wherever you have this, or use GergelyPolonkai's method to get a db object.
from typing import List, Dict, Union
from flask_sqlalchemy import DefaultMeta
from sqlalchemy.ext.declarative.clsregistry import _ModuleMarker
registered_classes: List[Union[DefaultMeta, _ModuleMarker]] = \
[x for x in db.Model._decl_class_registry.values()]
registered_models: List[DefaultMeta] = \
[x for x in registered_classes if isinstance(x, DefaultMeta)]
tables: List[DefaultMeta] = \
[x for x in registered_models if hasattr(x, '__tablename__')]
Upvotes: 0
Reputation: 6431
If you only need the classes, there is an simpler solution. I came up with it based on Celeo’s answer:
from flask import current_app
# This is to be generic (ie. no need to pass your actual SQLAlchemy instance)
# This way you can include it even in your own extension.
db = current_app.extensions['sqlalchemy'].db
[cls for cls in db.Model._decl_class_registry.values()
if isinstance(cls, type) and issubclass(cls, db.Model)]
This doesn’t need all those extra helper variables, just queries the class registry, and filters out everything that is not a model.
Upvotes: 11
Reputation: 16660
this works in my case: slightly shorter; more readable
models = []
for name, thing in db_table_creation.__dict__.iteritems():
if isinstance(thing, sqlalchemy.ext.declarative.DeclarativeMeta) and hasattr(thing, '__tablename__'):
models.append(thing)
Upvotes: 0
Reputation: 5682
There are several related questions, but I didn't see any exact duplicates.
Using your code to get the table names, and this question's accepted answer to get the classes, we can match the classes to the tablenames that are registered on your database.
classes, models, table_names = [], [], []
for clazz in db.Model._decl_class_registry.values():
try:
table_names.append(clazz.__tablename__)
classes.append(clazz)
except:
pass
for table in db.metadata.tables.items():
if table[0] in table_names:
models.append(classes[table_names.index(table[0])])
Where models
is the list of models registed on your database.
The try catch is required because a <sqlalchemy.ext.declarative.clsregistry._ModuleMarker object>
will be included in the for clazz in ...
loop, and it doesn't have a __tablename__
attribute.
Upvotes: 14