The QDjango object relational mapper (ORM) supports the concept of querysets, borrowed from django's ORM.
A queryset is a collection of database objects which match a certain number of user-specified conditions.
You can learn more about querysets by reading the QDjangoQuerySet template class documentation.
Creating and filtering querysets
Before you can start using querysets, you need to declare your database models as described in Database models.
The most basic queryset matches all the objects for a given model.
The QDjangoQuerySet class is a template class for performing database queries.
Definition QDjangoQuerySet.h:47
You can use the QDjangoQuerySet::filter() and QDjangoQuerySet::exclude() methods to add filtering conditions to a querset:
QDjangoQuerySet filter(const QDjangoWhere &where) const
Definition QDjangoQuerySet.h:483
The QDjangoWhere class expresses an SQL constraint.
Definition QDjangoWhere.h:41
@ StartsWith
Returns true if the column value starts with the given value (strings only).
Definition QDjangoWhere.h:52
@ NotEquals
Returns true if the column value is not equal to the given value.
Definition QDjangoWhere.h:47
@ Equals
Returns true if the column value is equal to the given value.
Definition QDjangoWhere.h:46
You can also use the QDjangoQuerySet::limit() method to limit the number of returned rows:
someUsers = users.
limit(0, 100);
QDjangoQuerySet limit(int pos, int length=-1) const
Definition QDjangoQuerySet.h:521
Iterating over results
The easiest way to iterate over results is to use Qt's foreach keyword:
foreach (const User &user, someUsers) {
qDebug() << "found user" << user.username;
}
Another way of iterating over results is to run over model instances using the QDjangoQuerySet::size() and QDjangoQuerySet::at() methods:
User user;
for (
int i = 0; i < someUsers.
size(); ++i) {
if (someUsers.
at(i, &user)) {
qDebug() << "found user" << user.username;
}
}
T * at(int index, T *target=0)
Definition QDjangoQuerySet.h:364
int size()
Definition QDjangoQuerySet.h:596
It is also possible to retrieve field data without creating model instances using the QDjangoQuerySet::values() and QDjangoQuerySet::valuesList() methods:
QList<QVariantMap> propertyMaps = someUsers.
values(QStringList() <<
"username" <<
"password");
foreach (const QVariantMap &propertyMap, propertyMaps) {
qDebug() << "username" << propertyList["username"];
qDebug() << "password" << propertyList["password"];
}
QList<QVariantList> propertyLists = someUsers.
valuesList(QStringList() <<
"username" <<
"password");
foreach (const QVariantList &propertyList, propertyLists) {
qDebug() << "username" << propertyList[0];
qDebug() << "password" << propertyList[1];
}
QList< QVariantList > valuesList(const QStringList &fields=QStringList())
Definition QDjangoQuerySet.h:630
QList< QVariantMap > values(const QStringList &fields=QStringList())
Definition QDjangoQuerySet.h:618
Other operations
int numberOfUsers = someUsers.
count();
int count() const
Definition QDjangoQuerySet.h:442
bool remove()
Definition QDjangoQuerySet.h:572