Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def prepare_models():
metadata = get_meta()
models = [model for model in get_all_django_models() if not model._meta.proxy]
Cache.sa_models = construct_models(metadata)
cache_models = {}
for model in models:
table_name = (
metadata.schema + '.' + model._meta.db_table
if metadata.schema else model._meta.db_table
)
cache_models[table_name] = Cache.sa_models[model]
model.sa = Cache.sa_models[model]
# Assign the deprecated attribute to the cache all at once
Cache.models = cache_models
sa_model = type(model._meta.object_name, bases,
{'table': table,
'alias': router.db_for_read(model)})
sa_models_by_table_names[table_name] = sa_model
sa_models_by_django_models[model] = sa_model
for model in models:
sa_model = sa_models_by_django_models[model]
table = tables[model._meta.db_table]
attrs = _extract_model_attrs(model, sa_models_by_django_models)
orm.mapper(sa_model, table, attrs)
model.sa = sa_model
Cache.sa_models = sa_models_by_django_models
Cache.models = sa_models_by_table_names