我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.db.connection.settings_dict()。
def get_stats_size(self): stats_size = LazyStruct() from django.db import connection cursor = connection.cursor() if 'postgres' in connection.settings_dict['ENGINE']: stats_size.row_count = stats_size.data = stats_size.index = 0 for model in Stats: cursor.execute("select count(id) as rows, pg_relation_size('{0}') as data_length, pg_total_relation_size('{0}') - pg_relation_size('{0}') as index_length from {0}".format(model._meta.db_table)) rows, data, index = cursor.fetchone() stats_size.row_count += rows stats_size.data += data stats_size.index += index else: raise RuntimeError("Unsupported DB: %s" % connection.settings_dict['ENGINE']) return stats_size
def patch_test_db(testing): """ Reference: django/db/backends/base/creation.py:57 A context manager to modify current database connection to simulate a test environment, just as what `manage.py test` does. The helper will be useful when migrating models for test database. """ old_db_name = connection.settings_dict['NAME'] test_db_name = connection.creation._get_test_db_name() alias = connection.alias _set_database_name(testing, alias, test_db_name) yield _set_database_name(testing, alias, old_db_name)
def make_database_faster(self): '''Sacrifices some of the safeguards of sqlite3 for speed Users are not likely to run this command in a production environment. They are even less likely to run it in production while using sqlite3. ''' if 'sqlite3' in connection.settings_dict['ENGINE']: cursor = connection.cursor() cursor.execute('PRAGMA temp_store = MEMORY;') cursor.execute('PRAGMA synchronous = OFF;')
def __init__(self): self.db_name = connection.settings_dict['NAME'] print(self.db_name) self.team = None self.cache = None self.player = None self.season = None self.ladder = None self.ranking = None self.ranking_data = None self.default_ranking_data__data = {} self.clear_defaults()
def _set_database_name(testing, alias, db_name): if not testing: return connection.close() settings.DATABASES[alias]['NAME'] = db_name connection.settings_dict['NAME'] = db_name