Class BaseDatabaseWrapper
Represent a database connection.
Declaration
class BaseDatabaseWrapper
source linkDocumentation
Methods
▶ def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS) Overrides
This method is overriden in:
@property
def allow_thread_sharing(self)
▶ 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.
Overrides
This method is overriden in:
▶ 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.
Overrides
This method is overriden in:
▷ def clean_savepoints(self) @async_unsafe Reset the counter used to generate unique savepoint ids in this thread.
@async_unsafe
def clean_savepoints(self)
▶ def close(self) @async_unsafe Close the connection to the database.
@async_unsafe
def close(self)
Overrides
This method is overriden in:
▷ def commit(self) @async_unsafe Commit a transaction and reset the dirty flag.
@async_unsafe
def commit(self)
▷ def connect(self) @async_unsafe Connect to the database. Assume that the connection is closed.
@async_unsafe
def connect(self)
@contextmanager
def constraint_checks_disabled(self)
▶ def copy(self, alias=None) Return a copy of this connection.
For tests that require two connections to the same database.
▶ def create_cursor(self, name=None) Create a cursor. Assume that a connection is established.
Overrides
This method is overriden in:
▷ def cursor(self) @async_unsafe Create a cursor, opening a connection if necessary.
@async_unsafe
def cursor(self)
▶ 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.
Overrides
This method is overriden in:
Overrides
This method is overriden in:
▷ def ensure_connection(self) @async_unsafe Guarantee that a connection to the database is established.
@async_unsafe
def ensure_connection(self)
▶ def ensure_timezone(self) Ensure the connection's timezone is set to `self.timezone_name` and return whether it changed or not.
Overrides
This method is overriden in:
▷ def execute_wrapper(self, wrapper) @contextmanager Return a context manager under which the wrapper is applied to suitable database query executions.
@contextmanager
def execute_wrapper(
self,
wrapper,
)
Overrides
This method is overriden in:
Overrides
This method is overriden in:
▷ def get_rollback(self) Get the "needs rollback" flag -- for *advanced use* only.
Overrides
This method is overriden in:
▶ 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.
Overrides
This method is overriden in:
▷ 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.
Overrides
This method is overriden in:
▶ def prepare_database(self) Hook to do any database check or preparation, generally called before migrating a project or an app.
Overrides
This method is overriden in:
@property
def queries(self)
@property
def queries_logged(self)
▷ def rollback(self) @async_unsafe Roll back a transaction and reset the dirty flag.
@async_unsafe
def rollback(self)
▷ def savepoint(self) @async_unsafe 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(self)
▷ def savepoint_commit(self, sid) @async_unsafe Release a savepoint. Do nothing if savepoints are not supported.
@async_unsafe
def savepoint_commit(
self,
sid,
)
▷ def savepoint_rollback(self, sid) @async_unsafe Roll back to a savepoint. Do nothing if savepoints are not supported.
@async_unsafe
def savepoint_rollback(
self,
sid,
)
▷ 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.
▶ def temporary_connection(self) @contextmanager 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.
@contextmanager
def temporary_connection(self)
Provide a cursor: with self.temporary_connection() as cursor: ...
▶ def timezone(self) @cached_property Return a tzinfo of the database connection time zone.
@cached_property
def timezone(self)
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.
▷ def timezone_name(self) @cached_property Name of the time zone of the database connection.
@cached_property
def timezone_name(self)
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.
▷ def wrap_database_errors(self) @cached_property Context manager and decorator that re-throws backend-specific database exceptions using Django's common wrappers.
@cached_property
def wrap_database_errors(self)
Subclasses
Reexports