QDjango
Scripting models

The QDjangoScript class makes it easy to access your models from QtScript.

Making your models scriptable

You can register a model with a QScriptEngine instance as follows:

#include <QDjangoQuerySet.h>
#include <QDjangoScript.h>

Q_DECLARE_METATYPE(QDjangoQuerySet<User>)

QScriptEngine *engine = new QScriptEngine;
QDjangoScript::registerWhere(engine);
QDjangoScript::registerModel<User>(engine);

Using your models from a script

Because QDjango makes use of Qt's property system, all model fields can automatically be accessed from QtScript. For instance if you declared a User model, you can run the following code:

// create a user instance and save it to database
user = new User();
user.username = "someuser";
user.password = "somepassword";
user.save();

// remove the user from database
user.remove();

You can also perform database queries:

// filter users whose username is "foouser" and password is "foopass"
qs = User.objects.filter({'username': 'foouser', 'password': 'foopass'});

// iterate over the results
for (var i = 0; i < qs.size(); i++) {
  user = qs.at(i);
  print("found " + user.username);
}

// remove all matching users from database
qs.remove();
 All Classes Functions Typedefs Enumerations Enumerator Properties