class documentation

class BaseDatabaseWrapper:

Known subclasses: django.db.backends.dummy.base.DatabaseWrapper, django.db.backends.mysql.base.DatabaseWrapper, django.db.backends.oracle.base.DatabaseWrapper, django.db.backends.postgresql.base.DatabaseWrapper, django.db.backends.sqlite3.base.DatabaseWrapper

View In Hierarchy

Represent a database connection.
Method __init__ Undocumented
Method ​_close Undocumented
Method ​_commit Undocumented
Method ​_cursor Undocumented
Method ​_nodb​_cursor No summary
Method ​_prepare​_cursor Validate the connection is usable and perform database cursor wrapping.
Method ​_rollback Undocumented
Method ​_savepoint Undocumented
Method ​_savepoint​_allowed Undocumented
Method ​_savepoint​_commit Undocumented
Method ​_savepoint​_rollback Undocumented
Method ​_set​_autocommit Backend-specific implementation to enable or disable autocommit.
Method check​_constraints Backends can override this method if they can apply constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE"). Should raise an IntegrityError if any invalid foreign key references are encountered.
Method check​_settings Undocumented
Method chunked​_cursor Return a cursor that tries to avoid caching in the database (if supported by the database), otherwise return a regular cursor.
Method clean​_savepoints Reset the counter used to generate unique savepoint ids in this thread.
Method close Close the connection to the database.
Method close​_if​_unusable​_or​_obsolete Close the current connection if unrecoverable errors have occurred or if it outlived its maximum age.
Method commit Commit a transaction and reset the dirty flag.
Method connect Connect to the database. Assume that the connection is closed.
Method constraint​_checks​_disabled Disable foreign key constraint checking.
Method copy Return a copy of this connection.
Method create​_cursor Create a cursor. Assume that a connection is established.
Method cursor Create a cursor, opening a connection if necessary.
Method dec​_thread​_sharing Undocumented
Method disable​_constraint​_checking Backends can implement as needed to temporarily disable foreign key constraint checking. Should return True if the constraints were disabled and will need to be reenabled.
Method enable​_constraint​_checking Backends can implement as needed to re-enable foreign key constraint checking.
Method ensure​_connection Guarantee that a connection to the database is established.
Method ensure​_timezone Ensure the connection's timezone is set to self.timezone_name and return whether it changed or not.
Method execute​_wrapper Return a context manager under which the wrapper is applied to suitable database query executions.
Method get​_autocommit Get the autocommit state.
Method get​_connection​_params Return a dict of parameters suitable for get_new_connection.
Method get​_new​_connection Open a connection to the database.
Method get​_rollback Get the "needs rollback" flag -- for advanced use only.
Method inc​_thread​_sharing Undocumented
Method init​_connection​_state Initialize the database connection settings.
Method is​_usable Test if the database connection is usable.
Method make​_cursor Create a cursor without debug logging.
Method make​_debug​_cursor Create a cursor that logs all queries in self.queries_log.
Method on​_commit Undocumented
Method prepare​_database Hook to do any database check or preparation, generally called before migrating a project or an app.
Method rollback Roll back a transaction and reset the dirty flag.
Method run​_and​_clear​_commit​_hooks Undocumented
Method savepoint Create a savepoint inside the current transaction. Return an identifier for the savepoint that will be used for the subsequent rollback or commit. Do nothing if savepoints are not supported.
Method savepoint​_commit Release a savepoint. Do nothing if savepoints are not supported.
Method savepoint​_rollback Roll back to a savepoint. Do nothing if savepoints are not supported.
Method schema​_editor Return a new instance of this backend's SchemaEditor.
Method set​_autocommit Enable or disable autocommit.
Method set​_rollback Set or unset the "needs rollback" flag -- for advanced use only.
Method temporary​_connection Context manager that ensures that a connection is established, and if it opened one, closes it to avoid leaving a dangling connection. This is useful for operations outside of the request-response cycle.
Method validate​_no​_atomic​_block Raise an error if an atomic block is active.
Method validate​_no​_broken​_transaction Undocumented
Method validate​_thread​_sharing No summary
Class Variable client​_class Undocumented
Class Variable creation​_class Undocumented
Class Variable data​_type​_check​_constraints Undocumented
Class Variable data​_types Undocumented
Class Variable data​_types​_suffix Undocumented
Class Variable display​_name Undocumented
Class Variable features​_class Undocumented
Class Variable introspection​_class Undocumented
Class Variable ops​_class Undocumented
Class Variable queries​_limit Undocumented
Class Variable ​Schema​Editor​Class Undocumented
Class Variable vendor Undocumented
Instance Variable ​_thread​_ident Undocumented
Instance Variable ​_thread​_sharing​_count Undocumented
Instance Variable ​_thread​_sharing​_lock Undocumented
Instance Variable alias Undocumented
Instance Variable autocommit Undocumented
Instance Variable client Undocumented
Instance Variable close​_at Undocumented
Instance Variable closed​_in​_transaction Undocumented
Instance Variable commit​_on​_exit Undocumented
Instance Variable connection Undocumented
Instance Variable creation Undocumented
Instance Variable errors​_occurred Undocumented
Instance Variable execute​_wrappers Undocumented
Instance Variable features Undocumented
Instance Variable force​_debug​_cursor Undocumented
Instance Variable in​_atomic​_block Undocumented
Instance Variable introspection Undocumented
Instance Variable needs​_rollback Undocumented
Instance Variable ops Undocumented
Instance Variable queries​_log Undocumented
Instance Variable run​_commit​_hooks​_on​_set​_autocommit​_on Undocumented
Instance Variable run​_on​_commit Undocumented
Instance Variable savepoint​_ids Undocumented
Instance Variable savepoint​_state Undocumented
Instance Variable settings​_dict Undocumented
Instance Variable validation Undocumented
Property allow​_thread​_sharing Undocumented
Property queries Undocumented
Property queries​_logged Undocumented
Property timezone Return a tzinfo of the database connection time zone.
Property timezone​_name Name of the time zone of the database connection.
Property wrap​_database​_errors Context manager and decorator that re-throws backend-specific database exceptions using Django's common wrappers.
def _close(self):

Undocumented

def _commit(self):
def _cursor(self, name=None):

Undocumented

@contextmanager
def _nodb_cursor(self):
Return a cursor from an alternative connection to be used when there is no need to access the main database, specifically for test db creation/deletion. This also prevents the production database from being exposed to potential child threads while (or after) the test database is destroyed. Refs #10868, #17786, #16969.
def _prepare_cursor(self, cursor):
Validate the connection is usable and perform database cursor wrapping.
def _rollback(self):
def _savepoint(self, sid):

Undocumented

def _savepoint_allowed(self):
def _savepoint_commit(self, sid):
def _savepoint_rollback(self, sid):

Undocumented

def check_constraints(self, table_names=None):
Backends can override this method if they can apply constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE"). Should raise an IntegrityError if any invalid foreign key references are encountered.
def check_settings(self):

Undocumented

def chunked_cursor(self):
Return a cursor that tries to avoid caching in the database (if supported by the database), otherwise return a regular cursor.
@async_unsafe
def clean_savepoints(self):
Reset the counter used to generate unique savepoint ids in this thread.
@async_unsafe
def close(self):
Close the connection to the database.
def close_if_unusable_or_obsolete(self):
Close the current connection if unrecoverable errors have occurred or if it outlived its maximum age.
@async_unsafe
def commit(self):
Commit a transaction and reset the dirty flag.
@async_unsafe
def connect(self):
Connect to the database. Assume that the connection is closed.
@contextmanager
def constraint_checks_disabled(self):
Disable foreign key constraint checking.
def copy(self, alias=None):

Return a copy of this connection.

For tests that require two connections to the same database.

@async_unsafe
def cursor(self):
Create a cursor, opening a connection if necessary.
def dec_thread_sharing(self):

Undocumented

def disable_constraint_checking(self):
Backends can implement as needed to temporarily disable foreign key constraint checking. Should return True if the constraints were disabled and will need to be reenabled.
def enable_constraint_checking(self):
Backends can implement as needed to re-enable foreign key constraint checking.
@async_unsafe
def ensure_connection(self):
Guarantee that a connection to the database is established.
def ensure_timezone(self):
Ensure the connection's timezone is set to self.timezone_name and return whether it changed or not.
@contextmanager
def execute_wrapper(self, wrapper):
Return a context manager under which the wrapper is applied to suitable database query executions.
def get_autocommit(self):
Get the autocommit state.
def get_rollback(self):
Get the "needs rollback" flag -- for advanced use only.
def inc_thread_sharing(self):

Undocumented

def is_usable(self):

Test if the database connection is usable.

This method may assume that self.connection is not None.

Actual implementations should take care not to raise exceptions as that may prevent Django from recycling unusable connections.

def make_cursor(self, cursor):
Create a cursor without debug logging.
def make_debug_cursor(self, cursor):
Create a cursor that logs all queries in self.queries_log.
def on_commit(self, func):

Undocumented

def prepare_database(self):
Hook to do any database check or preparation, generally called before migrating a project or an app.
@async_unsafe
def rollback(self):
Roll back a transaction and reset the dirty flag.
def run_and_clear_commit_hooks(self):

Undocumented

@async_unsafe
def savepoint(self):
Create a savepoint inside the current transaction. Return an identifier for the savepoint that will be used for the subsequent rollback or commit. Do nothing if savepoints are not supported.
@async_unsafe
def savepoint_commit(self, sid):
Release a savepoint. Do nothing if savepoints are not supported.
@async_unsafe
def savepoint_rollback(self, sid):
Roll back to a savepoint. Do nothing if savepoints are not supported.
def schema_editor(self, *args, **kwargs):
Return a new instance of this backend's SchemaEditor.
def set_autocommit(self, autocommit, force_begin_transaction_with_broken_autocommit=False):

Enable or disable autocommit.

The usual way to start a transaction is to turn autocommit off. SQLite does not properly start a transaction when disabling autocommit. To avoid this buggy behavior and to actually enter a new transaction, an explicit BEGIN is required. Using force_begin_transaction_with_broken_autocommit=True will issue an explicit BEGIN with SQLite. This option will be ignored for other backends.

def set_rollback(self, rollback):
Set or unset the "needs rollback" flag -- for advanced use only.
@contextmanager
def temporary_connection(self):

Context manager that ensures that a connection is established, and if it opened one, closes it to avoid leaving a dangling connection. This is useful for operations outside of the request-response cycle.

Provide a cursor: with self.temporary_connection() as cursor: ...

def validate_no_atomic_block(self):
Raise an error if an atomic block is active.
def validate_no_broken_transaction(self):

Undocumented

def validate_thread_sharing(self):
Validate that the connection isn't accessed by another thread than the one which originally created it, unless the connection was explicitly authorized to be shared between threads (via the inc_thread_sharing() method). Raise an exception if the validation fails.
client_class =

Undocumented

creation_class =

Undocumented

data_types_suffix: dict =
features_class =

Undocumented

introspection_class =

Undocumented

ops_class =

Undocumented

queries_limit: int =

Undocumented

SchemaEditorClass =

Undocumented

_thread_ident =

Undocumented

_thread_sharing_count: int =

Undocumented

_thread_sharing_lock =

Undocumented

alias =

Undocumented

autocommit =

Undocumented

client =

Undocumented

close_at =

Undocumented

closed_in_transaction: bool =

Undocumented

commit_on_exit: bool =

Undocumented

connection =

Undocumented

creation =

Undocumented

errors_occurred: bool =

Undocumented

execute_wrappers: list =

Undocumented

force_debug_cursor: bool =

Undocumented

in_atomic_block: bool =

Undocumented

introspection =
needs_rollback =
queries_log =

Undocumented

run_commit_hooks_on_set_autocommit_on: bool =

Undocumented

run_on_commit: list =

Undocumented

savepoint_ids: list =

Undocumented

savepoint_state: int =

Undocumented

settings_dict =

Undocumented

validation =

Undocumented

@property
allow_thread_sharing =

Undocumented

@property
queries =

Undocumented

@property
queries_logged =

Undocumented

@cached_property
timezone =

Return a tzinfo of the database connection time zone.

This is only used when time zone support is enabled. When a datetime is read from the database, it is always returned in this time zone.

When the database backend supports time zones, it doesn't matter which time zone Django uses, as long as aware datetimes are used everywhere. Other users connecting to the database can choose their own time zone.

When the database backend doesn't support time zones, the time zone Django uses may be constrained by the requirements of other users of the database.

@cached_property
timezone_name =
Name of the time zone of the database connection.
@cached_property
wrap_database_errors =
Context manager and decorator that re-throws backend-specific database exceptions using Django's common wrappers.