1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
/*
* Copyright (C) 2010-2012 Jeremy Lainé
* Contact: http://code.google.com/p/qdjango/
*
* This file is part of the QDjango Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
/*! \page scripting Scripting models
The QDjangoScript class makes it easy to access your models from <a href="http://doc.qt.nokia.com/latest/qtscript.html">QtScript</a>.
\section making-scriptable Making your models scriptable
You can register a model with a <a href="http://doc.qt.nokia.com/latest/qscriptengine.html">QScriptEngine</a> instance as follows:
\code
#include <QDjangoQuerySet.h>
#include <QDjangoScript.h>
Q_DECLARE_METATYPE(QDjangoQuerySet<User>)
QScriptEngine *engine = new QScriptEngine;
QDjangoScript::registerWhere(engine);
QDjangoScript::registerModel<User>(engine);
\endcode
\section scripting-models Using your models from a script
Because QDjango makes use of Qt's property system, all model fields can
automatically be accessed from <a href="http://doc.qt.nokia.com/latest/qtscript.html">QtScript</a>.
For instance if you declared a \c User model, you can run the following code:
\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();
\endcode
You can also perform database queries:
\code
// 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();
\endcode
*/
|