File: models.doc

package info (click to toggle)
qdjango 0.6.2-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 908 kB
  • sloc: cpp: 7,818; sh: 32; makefile: 12; javascript: 1
file content (132 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2010-2015 Jeremy Lainé
 * Contact: https://github.com/jlaine/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 models Database models

Database models are usually created by subclassing the QDjangoModel class.

The following example defines a \c User model suitable for storing basic
account information, and illustrate different types of queries on this model.

\section declaring Declaring your model

To declare your model, subclass the QDjangoModel class, and define a property
using the <a href="http://doc.qt.io/qt-5/qobject.html#Q_PROPERTY">Q_PROPERTY</a>
macro for each database field. You can provide additional information about a
field using the <a href="http://doc.qt.io/qt-5/qobject.html#Q_CLASSINFO">Q_CLASSINFO</a>
macro:

 \li \c max_length : the maximum length of the field (used when creating the database table)
 \li \c 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.

\code
#include "QDjangoModel.h"

class User : public QDjangoModel
{
    Q_OBJECT
    Q_PROPERTY(QString username READ username WRITE setUsername)
    Q_PROPERTY(QString password READ password WRITE setPassword)

    Q_CLASSINFO("username", "max_length=255")
    Q_CLASSINFO("password", "max_length=128")

public:
    QString username() const;
    void setUsername(const QString &username);

    QString password() const;
    void setPassword(const QString &password);

private:
    QString m_username;
    QString m_password;
};
\endcode

\section implementing Implementing your model

\code
QString User::username() const
{
    return m_username;
}

void User::setUsername(const QString &username)
{
    m_username = username;
}

QString User::password() const
{
    return m_password;
}

void User::setPassword(const QString &password)
{
    m_password = password;
}
\endcode

\section registering Registering and using your model

To make your model available for database operations, you should now register your model using:

\code
QDjango::registerModel<User>();
\endcode

Once you have set the database (see \ref database), you will now be able to create model instances and save them to the database:

\code
User *user = new User;
user->setUsername("someuser");
user->setPassword("somepassword");
user->save();
\endcode

.. or remove them from the database:

\code
user->remove();
\endcode

You can also perform operations such as filtering or retrieving model
instances as described in \ref queries.

\section qobject Using QDjango without QDjangoModel

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 \c SomeObject class which inherits <a href="http://doc.qt.io/qt-5/qobject.html">QObject</a>, you can write:

\code
QDjangoMetaModel meta = QDjango::registerModel<SomeObject>();

// prepare a SomeObject instance
SomeObject *obj = new SomeObject;
obj->setSomeProperty("some value");
obj->setOtherProperty("other value");

// save the object
meta.save(obj);

// remove the object from database
meta.remove(obj);
\endcode

*/