本文整理汇总了Python中django.db.connection方法的典型用法代码示例。如果您正苦于以下问题:Python db.connection方法的具体用法?Python db.connection怎么用?Python db.connection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db
的用法示例。
在下文中一共展示了db.connection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: migrations
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def migrations(transactional_db):
"""
This fixture returns a helper object to test Django data migrations.
Based on: https://gist.github.com/bennylope/82a6088c02fefdd47e18f3c04ec167af
"""
class Migrator(object):
def migrate(self, app, to):
migration = [(app, to)]
executor = MigrationExecutor(connection)
executor.migrate(migration)
return executor.loader.project_state(migration).apps
def reset(self):
call_command("migrate", no_input=True)
return Migrator()
示例2: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setUp(self):
assert (
self.migrate_from and self.migrate_to
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例3: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setUp(self):
assert self.migrate_from and self.migrate_to, \
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例4: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setUp(self) -> None:
assert self.migrate_from and self.migrate_to, \
f"TestCase '{type(self).__name__}' must define migrate_from and migrate_to properties"
migrate_from: List[Tuple[str, str]] = [(self.app, self.migrate_from)]
migrate_to: List[Tuple[str, str]] = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(migrate_from).apps
# Reverse to the original migration
executor.migrate(migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(migrate_to)
self.apps = executor.loader.project_state(migrate_to).apps
示例5: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setUp(self):
assert (
self.migrate_from and self.migrate_to
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
if self.migrate_fixtures:
self.load_fixtures(self.migrate_fixtures, apps=old_apps)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例6: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setUp(self):
self.migrate_from = [self.migrate_from]
self.migrate_to = [self.migrate_to]
# Reverse to the original migration
executor = MigrationExecutor(connection)
executor.migrate(self.migrate_from)
old_apps = executor.loader.project_state(self.migrate_from).apps
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor.loader.build_graph()
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例7: to_df
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def to_df(queryset):
"""
:param queryset: django.db.models.query.QuerySet
:return: pandas.core.frame.DataFrame
"""
try:
query, params = queryset.query.sql_with_params()
except EmptyResultSet:
# Occurs when Django tries to create an expression for a
# query which will certainly be empty
# e.g. Book.objects.filter(author__in=[])
return pd.DataFrame()
return read_sql_query(query, connection, params=params)
示例8: test_makes_no_db_queries
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def test_makes_no_db_queries(self, client):
queries = CaptureQueriesContext(connection)
with queries:
res = client.get("/api/v1/classify_client/")
assert res.status_code == 200
assert len(queries) == 0
示例9: test_apis_make_a_reasonable_number_of_db_queries
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def test_apis_make_a_reasonable_number_of_db_queries(client, endpoint, Factory):
"""
Naive versions of these views could easily make several queries
per item, which is very slow. Make sure that isn't the case.
"""
Factory.create_batch(100)
queries = CaptureQueriesContext(connection)
with queries:
res = client.get(endpoint)
assert res.status_code == 200
# Anything under 100 isn't doing one query per recipe.
assert len(queries) < 100
示例10: test_apis_makes_a_reasonable_number_of_db_queries
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def test_apis_makes_a_reasonable_number_of_db_queries(endpoint, Factory, client, settings):
# Naive versions of this view could easily make several queries
# per item, which is very slow. Make sure that isn't the case.
Factory.create_batch(100)
queries = CaptureQueriesContext(connection)
with queries:
res = client.get(endpoint)
assert res.status_code == 200
# Pagination naturally makes one query per item in the page. Anything
# under `page_size * 2` isn't doing any additional queries per recipe.
page_size = settings.REST_FRAMEWORK["PAGE_SIZE"]
assert len(queries) < page_size * 2, queries
示例11: test_makes_no_db_queries
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def test_makes_no_db_queries(self, client):
queries = CaptureQueriesContext(connection)
with queries:
url = reverse("selfrepair:index", args=["en-US"])
res = client.get(url)
assert res.status_code == 200
assert len(queries) == 0
示例12: tearDown
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def tearDown(self):
executor = MigrationExecutor(connection)
executor.loader.build_graph()
executor.migrate(executor.loader.graph.leaf_nodes(self.app))
示例13: setup_db
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def setup_db(self):
"""
Drops and re-installs the database found at "arches_<package_name>"
WARNING: This will destroy data
"""
conninfo = self.get_connection()
conn = conninfo["connection"]
can_create_db = conninfo["can_create_db"]
cursor = conn.cursor()
if can_create_db is True:
self.drop_and_recreate_db(cursor)
else:
self.reset_db(cursor)
# delete existing indexes
management.call_command("es", operation="delete_indexes")
# setup initial Elasticsearch indexes
management.call_command("es", operation="setup_indexes")
management.call_command("migrate")
createcachetable = False
for k, v in settings.CACHES.items():
if v["BACKEND"] == "django.core.cache.backends.db.DatabaseCache":
createcachetable = True
if createcachetable:
management.call_command("createcachetable")
# import system settings graph and any saved system settings data
settings_graph = os.path.join(settings.ROOT_DIR, "db", "system_settings", "Arches_System_Settings_Model.json")
management.call_command("packages", operation="import_graphs", source=settings_graph)
settings_data = os.path.join(settings.ROOT_DIR, "db", "system_settings", "Arches_System_Settings.json")
management.call_command("packages", operation="import_business_data", source=settings_data, overwrite=True)
settings_data_local = settings.SYSTEM_SETTINGS_LOCAL_PATH
if os.path.isfile(settings_data_local):
management.call_command("packages", operation="import_business_data", source=settings_data_local, overwrite=True)
示例14: test_URLListField_db_type
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def test_URLListField_db_type(self):
"""Make sure that URLListField has a proper type"""
links = URLListField()
self.assertEqual(links.db_parameters(connection)['type'], 'text')
示例15: check_field
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import connection [as 别名]
def check_field(self, field, **kwargs):
"""
MySQL has the following field length restriction:
No character (varchar) fields can have a length exceeding 255
characters if they have a unique index on them.
"""
# Django 1.7
errors = super(DatabaseValidation, self).check_field(field,
**kwargs)
# Ignore any related fields.
if getattr(field, 'rel', None) is None:
field_type = field.db_type(connection)
if field_type is None:
return errors
if (field_type.startswith('varchar') # Look for CharFields...
and field.unique # ... that are unique
and (field.max_length is None or
int(field.max_length) > 255)):
errors.append(
checks.Error(
('MySQL does not allow unique CharFields to have a '
'max_length > 255.'),
hint=None,
obj=field,
id='mysql.E001',
)
)
return errors