當前位置: 首頁>>代碼示例>>Python>>正文


Python db.connection方法代碼示例

本文整理匯總了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() 
開發者ID:mozilla,項目名稱:normandy,代碼行數:19,代碼來源:conftest.py

示例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 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:24,代碼來源:util.py

示例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 
開發者ID:yunity,項目名稱:karrot-backend,代碼行數:20,代碼來源:utils.py

示例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 
開發者ID:zulip,項目名稱:zulip,代碼行數:21,代碼來源:test_classes.py

示例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 
開發者ID:byro,項目名稱:byro,代碼行數:27,代碼來源:helper.py

示例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 
開發者ID:SimpleJWT,項目名稱:django-rest-framework-simplejwt,代碼行數:18,代碼來源:utils.py

示例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) 
開發者ID:BigBrotherTrade,項目名稱:trader,代碼行數:15,代碼來源:models.py

示例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 
開發者ID:mozilla,項目名稱:normandy,代碼行數:8,代碼來源:test_api.py

示例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 
開發者ID:mozilla,項目名稱:normandy,代碼行數:14,代碼來源:test_api.py

示例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 
開發者ID:mozilla,項目名稱:normandy,代碼行數:17,代碼來源:test_api.py

示例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 
開發者ID:mozilla,項目名稱:normandy,代碼行數:9,代碼來源:test_views.py

示例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)) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:6,代碼來源:util.py

示例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) 
開發者ID:archesproject,項目名稱:arches,代碼行數:43,代碼來源:setup_db.py

示例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') 
開發者ID:AdventureLookup,項目名稱:adventurelookup-backend,代碼行數:6,代碼來源:test_adventures.py

示例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 
開發者ID:LuciferJack,項目名稱:python-mysql-pool,代碼行數:33,代碼來源:validation.py


注:本文中的django.db.connection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。