QDjango
|
Database models are usually created by subclassing the QDjangoModel class.
The following example defines a User
model suitable for storing basic account information, and illustrate different types of queries on this model.
To declare your model, subclass the QDjangoModel class, and define a property using the Q_PROPERTY macro for each database field. You can provide additional information about a field using the Q_CLASSINFO macro:
max_length
: the maximum length of the field (used when creating the database table) primary_key
: if set to 'true', this field will be used as the primary key. If no primary key is explicitly defined, an auto-increment integer field will be added.To make your model available for database operations, you should now register your model using:
Once you have set the database (see Database configuration), you will now be able to create model instances and save them to the database:
.. or remove them from the database:
You can also perform operations such as filtering or retrieving model instances as described in Making queries.
Although it is recommended you make your models inherit QDjangoModel, it is not strictly necessary. QDjango can in fact handle any QObject-derived class, but you will lose some of the syntactic sugar.
If for instance you defined a SomeObject
class which inherits QObject, you can write: