QDjango
Database configuration

Setting the database

The first step is to open the database using QSqlDatabase::addDatabase(), for instance for an in-memory SQLite database:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();

You should now tell QDjango to use the database you just opened:

Creating or dropping database tables

Once you have set the database and declared all your models (see Database models), you can ask QDjango to create the database tables for all models:

Conversely, you can ask QDjango to drop the database tables for all models:

Threading support

Internally, QDjango calls the QDjango::database() method whenever it needs a handle to the database. This method will clone the database connection as needed if it is invoked from a different thread.

QDjango::dropTables
static bool dropTables()
Drops the database tables for all registered models.
Definition: QDjango.cpp:286
QDjango::setDatabase
static void setDatabase(QSqlDatabase database)
Sets the database used by QDjango.
Definition: QDjango.cpp:198
QDjango::createTables
static bool createTables()
Creates the database tables for all registered models.
Definition: QDjango.cpp:269