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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>QDjango: Database models</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">QDjango
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Database models </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Database models are usually created by subclassing the <a class="el" href="classQDjangoModel.html" title="The QDjangoModel class is the base class for all models.">QDjangoModel</a> class.</p>
<p>The following example defines a <code>User</code> model suitable for storing basic account information, and illustrate different types of queries on this model.</p>
<h2><a class="anchor" id="declaring"></a>
Declaring your model</h2>
<p>To declare your model, subclass the <a class="el" href="classQDjangoModel.html" title="The QDjangoModel class is the base class for all models.">QDjangoModel</a> class, and define a property using the <a href="http://doc.qt.nokia.com/latest/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.nokia.com/latest/qobject.html#Q_CLASSINFO">Q_CLASSINFO</a> macro:</p>
<ul>
<li><code>max_length</code> : the maximum length of the field (used when creating the database table) </li>
<li><code>primary_key</code> : 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.</li>
</ul>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include "QDjangoModel.h"</span>
<span class="keyword">class </span>User : <span class="keyword">public</span> <a class="code" href="classQDjangoModel.html" title="The QDjangoModel class is the base class for all models.">QDjangoModel</a>
{
Q_OBJECT
Q_PROPERTY(QString username READ username WRITE setUsername)
Q_PROPERTY(QString password READ password WRITE setPassword)
Q_CLASSINFO(<span class="stringliteral">"username"</span>, <span class="stringliteral">"max_length=255"</span>)
Q_CLASSINFO("password", "max_length=128")
public:
QString username() const;
<span class="keywordtype">void</span> setUsername(const QString &username);
QString password() const;
<span class="keywordtype">void</span> setPassword(const QString &password);
private:
QString m_username;
QString m_password;
};
</pre></div><h2><a class="anchor" id="implementing"></a>
Implementing your model</h2>
<div class="fragment"><pre class="fragment">QString User::username()<span class="keyword"> const</span>
<span class="keyword"></span>{
<span class="keywordflow">return</span> m_username;
}
<span class="keywordtype">void</span> User::setUsername(<span class="keyword">const</span> QString &username)
{
m_username = username;
}
QString User::password()<span class="keyword"> const</span>
<span class="keyword"></span>{
<span class="keywordflow">return</span> m_password;
}
<span class="keywordtype">void</span> User::setPassword(<span class="keyword">const</span> QString &password)
{
m_password = password;
}
</pre></div><h2><a class="anchor" id="registering"></a>
Registering and using your model</h2>
<p>To make your model available for database operations, you should now register your model using:</p>
<div class="fragment"><pre class="fragment">QDjango::registerModel<User>();
</pre></div><p>Once you have set the database (see <a class="el" href="database.html">Database configuration</a>), you will now be able to create model instances and save them to the database:</p>
<div class="fragment"><pre class="fragment">User *user = <span class="keyword">new</span> User;
user->setUsername(<span class="stringliteral">"someuser"</span>);
user->setPassword(<span class="stringliteral">"somepassword"</span>);
user->save();
</pre></div><p>.. or remove them from the database:</p>
<div class="fragment"><pre class="fragment">user->remove();
</pre></div><p>You can also perform operations such as filtering or retrieving model instances as described in <a class="el" href="queries.html">Making queries</a>.</p>
<h2><a class="anchor" id="qobject"></a>
Using QDjango without QDjangoModel</h2>
<p>Although it is recommended you make your models inherit <a class="el" href="classQDjangoModel.html" title="The QDjangoModel class is the base class for all models.">QDjangoModel</a>, it is not strictly necessary. <a class="el" href="classQDjango.html" title="The QDjango class provides a set of static functions.">QDjango</a> can in fact handle any QObject-derived class, but you will lose some of the syntactic sugar.</p>
<p>If for instance you defined a <code>SomeObject</code> class which inherits <a href="http://doc.qt.nokia.com/latest/qobject.html">QObject</a>, you can write:</p>
<div class="fragment"><pre class="fragment"><a class="code" href="classQDjangoMetaModel.html" title="The QDjangoMetaModel class holds the database schema for a model.">QDjangoMetaModel</a> meta = QDjango::registerModel<SomeObject>();
<span class="comment">// prepare a SomeObject instance</span>
SomeObject *obj = <span class="keyword">new</span> SomeObject;
obj->setSomeProperty(<span class="stringliteral">"some value"</span>);
obj->setOtherProperty(<span class="stringliteral">"other value"</span>);
<span class="comment">// save the object</span>
meta.<a class="code" href="classQDjangoMetaModel.html#a6837d4f7f1d89327516e2e9ad39bef32" title="Saves the given model instance to the database.">save</a>(obj);
<span class="comment">// remove the object from database</span>
meta.<a class="code" href="classQDjangoMetaModel.html#a3b0a36043675008e7114b420a73e3f62" title="Removes the given model instance from the database.">remove</a>(obj);
</pre></div> </div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Properties</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>
Generated on Wed May 16 2012 18:11:23 for QDjango by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>
</body>
</html>
|