-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model Layer in Django.html
265 lines (191 loc) · 13.1 KB
/
Model Layer in Django.html
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html lang="en"><head>
<title>Model Layer in Django</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="Model%20Layer%20in%20Django_files/style.css" media="all" type="text/css" rel="stylesheet">
<style type="text/css">#yddContainer{display:block;font-family:Microsoft YaHei;position:relative;width:100%;height:100%;top:-4px;left:-4px;font-size:12px;border:1px solid}#yddTop{display:block;height:22px}#yddTopBorderlr{display:block;position:static;height:17px;padding:2px 28px;line-height:17px;font-size:12px;color:#5079bb;font-weight:bold;border-style:none solid;border-width:1px}#yddTopBorderlr .ydd-sp{position:absolute;top:2px;height:0;overflow:hidden}.ydd-icon{left:5px;width:17px;padding:0px 0px 0px 0px;padding-top:17px;background-position:-16px -44px}.ydd-close{right:5px;width:16px;padding-top:16px;background-position:left -44px}#yddKeyTitle{float:left;text-decoration:none}#yddMiddle{display:block;margin-bottom:10px}.ydd-tabs{display:block;margin:5px 0;padding:0 5px;height:18px;border-bottom:1px solid}.ydd-tab{display:block;float:left;height:18px;margin:0 5px -1px 0;padding:0 4px;line-height:18px;border:1px solid;border-bottom:none}.ydd-trans-container{display:block;line-height:160%}.ydd-trans-container a{text-decoration:none;}#yddBottom{position:absolute;bottom:0;left:0;width:100%;height:22px;line-height:22px;overflow:hidden;background-position:left -22px}.ydd-padding010{padding:0 10px}#yddWrapper{color:#252525;z-index:10001;background:url(chrome-extension://eopjamdnofihpioajgfdikhhbobonhbb/ab20.png);}#yddContainer{background:#fff;border-color:#4b7598}#yddTopBorderlr{border-color:#f0f8fc}#yddWrapper .ydd-sp{background-image:url(chrome-extension://eopjamdnofihpioajgfdikhhbobonhbb/ydd-sprite.png)}#yddWrapper a,#yddWrapper a:hover,#yddWrapper a:visited{color:#50799b}#yddWrapper .ydd-tabs{color:#959595}.ydd-tabs,.ydd-tab{background:#fff;border-color:#d5e7f3}#yddBottom{color:#363636}#yddWrapper{min-width:250px;max-width:400px;}</style>
<script src="Model%20Layer%20in%20Django_files/a.js" async="" type="text/javascript"></script><script src="Model%20Layer%20in%20Django_files/analytics.js" async=""></script><script src="Model%20Layer%20in%20Django_files/embed.js" async="" type="text/javascript"></script></head>
<body>
<header>
<section class="logo">
<h1><a href="http://webcoursify.github.io/">WebDev Course</a></h1>
<h2 class="subheader">by Tyrone Cheung</h2>
</section>
<nav id="nav">
<ul>
<li><a href="http://webcoursify.github.io/about">ReadMe</a></li>
<li><a href="http://webdemo.zhangty.me/">Project Demo</a></li>
<li><a href="http://webcoursify.github.io/project_code">Download Project</a></li>
<li><a href="http://eepurl.com/_YOyb">Subscribe update</a></li>
<li><a href="mailto:firstprayer.zhang@gmail.com">Contact</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="content">
<article>
<h1>Model Layer in Django</h1>
<h5>Last modified: 12-23-2014</h5>
<p>In this chapter we’ll dive deeper into a web system’s core, <em>Model</em>. In Django, as well as many other web frameworks, <em>Model</em>
can be seen as a blackbox, whick encapsulates the implementation
details of data storage and retrieval and provides methods for upper
layers to manipulate data. Sometimes it might also be the layer which
encapsulates complex core logics, such as one method for “transfer money
from A to B and charge A for the fee” or one method for “send an email
to all registered users”, etc, while at other times these complex logic
might be encapsulated in another <em>Service</em> layer and the <em>Model</em> layer will focus on data storage and retrieval. </p>
<p>Most web systems use database as backend support for Model. We’re
using MySQL for this tutorial, as you configured before. The following
content assumes you have a basic understanding of relational database.
If you feel confused about concepts such as: <em>Table</em>, <em>Schema</em>, <em>Column</em>, <em>Row</em>, <em>ForeignKey</em>, <em>PrimaryKey</em>, <em>Index</em>, you can go to:</p>
<ul>
<li><a href="http://www.tutorialspoint.com/sql/sql-rdbms-concepts.htm">Basic concept</a></li>
<li><a href="http://docs.oracle.com/javase/tutorial/jdbc/overview/database.html">Oracle tutorial</a></li>
</ul>
<p>…and look for the meaning of the terms I mentioned. In these tutorials you may also learn about <strong>SQL</strong>,
a language used for data manipulation in database (which is not
essential if you’re just using Django because the encapsulation of
Django hide this part from developers, but is necessary for ones who
want to become real serious web developers—and we’ll have assignments
for that as well), if you’re not familiar with it yet.</p>
</article>
<div id="disqus_thread"><iframe verticalscrolling="no" horizontalscrolling="no" src="Model%20Layer%20in%20Django_files/a.html" style="width: 100% ! important; border: medium none ! important; overflow: hidden ! important; height: 614px ! important;" title="Disqus" tabindex="0" scrolling="no" allowtransparency="true" data-disqus-uid="2" id="dsq-2" frameborder="0" width="100%"></iframe></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'webcoursify'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</div>
<aside class="sidebar">
<section class="module">
<h2 style="margin-left: 24px;">FullStack Web Development</h2>
<ul class="lesson-list chapter-list">
<li data-url="/content/0-Preface.html">
<a href="http://webcoursify.github.io/content/0-Preface.html">
<span class="lesson-title">1 - Preface</span>
</a>
</li>
<li data-url="/content/1-DjangoBasic">
<a href="http://webcoursify.github.io/content/1-DjangoBasic">
<span class="lesson-title">2 - Django Basic</span>
</a>
<ul class="section-list">
<li data-url="/content/1-DjangoBasic/1-ClientServer.html"><a href="http://webcoursify.github.io/content/1-DjangoBasic/1-ClientServer.html">1 - Client-Server Model and MVC Design Pattern</a></li>
<li data-url="/content/1-DjangoBasic/2-DjangoMVC.html"><a href="http://webcoursify.github.io/content/1-DjangoBasic/2-DjangoMVC.html">2 - Django's MVC</a></li>
<li data-url="/content/1-DjangoBasic/3-Assignment.html"><a href="http://webcoursify.github.io/content/1-DjangoBasic/3-Assignment.html">3 - Assignment-Django Basic</a></li>
</ul>
</li>
<li data-url="/content/2-MVC-Controller">
<a href="http://webcoursify.github.io/content/2-MVC-Controller">
<span class="lesson-title">3 - Intro to Django's Controller</span>
</a>
<ul class="section-list">
<li data-url="/content/2-MVC-Controller/1-DjangoReqRes.html"><a href="http://webcoursify.github.io/content/2-MVC-Controller/1-DjangoReqRes.html">1 - Django's HttpRequest, HttpResponse, and Url dispatching</a></li>
<li data-url="/content/2-MVC-Controller/2-Assignment.html"><a href="http://webcoursify.github.io/content/2-MVC-Controller/2-Assignment.html">2 - Assignment for Implementing Controller</a></li>
</ul>
</li>
<li class="active" data-url="/content/3-MVC-Model">
<a href="http://webcoursify.github.io/content/3-MVC-Model">
<span class="lesson-title">4 - Model Layer in Django</span>
</a>
<ul class="section-list">
<li data-url="/content/3-MVC-Model/1-DjangoModel.html"><a href="http://webcoursify.github.io/content/3-MVC-Model/1-DjangoModel.html">1 - Using Model in Django</a></li>
<li data-url="/content/3-MVC-Model/2-FurtherDiscussion.html"><a href="http://webcoursify.github.io/content/3-MVC-Model/2-FurtherDiscussion.html">2 - Further discussion with regard to Model</a></li>
<li data-url="/content/3-MVC-Model/3-Assignment.html"><a href="http://webcoursify.github.io/content/3-MVC-Model/3-Assignment.html">3 - Assignments for Model</a></li>
</ul>
</li>
<li data-url="/content/4-MVC-View">
<a href="http://webcoursify.github.io/content/4-MVC-View">
<span class="lesson-title">5 - The View-User Interaction</span>
</a>
<ul class="section-list">
<li data-url="/content/4-MVC-View/2-DjangoTemplate.html"><a href="http://webcoursify.github.io/content/4-MVC-View/2-DjangoTemplate.html">1 - Template in Django</a></li>
<li data-url="/content/4-MVC-View/3-FormAjax.html"><a href="http://webcoursify.github.io/content/4-MVC-View/3-FormAjax.html">2 - Form, Ajax</a></li>
<li data-url="/content/4-MVC-View/4-RichEditor.html"><a href="http://webcoursify.github.io/content/4-MVC-View/4-RichEditor.html">3 - Using Rich Text Editor with Ajax</a></li>
<li data-url="/content/4-MVC-View/5-Assignment.html"><a href="http://webcoursify.github.io/content/4-MVC-View/5-Assignment.html">4 - Assignments for View</a></li>
</ul>
</li>
<li data-url="/content/5-Topic-User.html">
<a href="http://webcoursify.github.io/content/5-Topic-User.html">
<span class="lesson-title">6 - User Management in WebApp</span>
</a>
</li>
<li data-url="/content/6-ProjectSpecification.html">
<a href="http://webcoursify.github.io/content/6-ProjectSpecification.html">
<span class="lesson-title">7 - Capstone Project Specification</span>
</a>
</li>
<li data-url="/content/99-ExtraMaterials">
<a href="http://webcoursify.github.io/content/99-ExtraMaterials">
<span class="lesson-title">8 - Extra Tutorials</span>
</a>
<ul class="section-list">
<li data-url="/content/99-ExtraMaterials/1-IntroToGit.html"><a href="http://webcoursify.github.io/content/99-ExtraMaterials/1-IntroToGit.html">1 - Introduction to Git</a></li>
<li data-url="/content/99-ExtraMaterials/2-Deployment.html"><a href="http://webcoursify.github.io/content/99-ExtraMaterials/2-Deployment.html">2 - Deployment</a></li>
</ul>
</li>
</ul>
</section>
<footer>
Copyright 2015 Tyrone Cheung
</footer>
<footer>
<p>
<b>This course has been finished just recently, and there're a
lot of improvements pending. If you like it, or think it's useful, or
start to go through the course content, please <a href="mailto:zhangty10@gmail.com">shoot me an email</a> to let me know. Thanks!</b>
</p>
</footer>
</aside>
<!-- Bootstrap core JavaScript-->
<!-- <script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/hc.js"></script> -->
<script type="text/javascript" src="Model%20Layer%20in%20Django_files/jquery-2.js"></script>
<script type="text/javascript" async="" src="Model%20Layer%20in%20Django_files/saved_resource.js"></script>
<script type="text/javascript" async="" src="Model%20Layer%20in%20Django_files/inpage_linkid.js" id="undefined"></script>
<script type="text/javascript" async="" src="Model%20Layer%20in%20Django_files/library.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l=document.getElementsByTagName("a"),t=document.createElement("textarea");for(i=0;l.length-i;i++){try{a=l[i].getAttribute("href");if(a&&a.indexOf("/cdn-cgi/l/email-protection") > -1 && (a.length > 28)){s='';j=27+ 1 + a.indexOf("/cdn-cgi/l/email-protection");if (a.length > j) {r=parseInt(a.substr(j,2),16);for(j+=2;a.length>j&&a.substr(j,1)!='X';j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}j+=1;s+=a.substr(j,a.length-j);}t.innerHTML=s.replace(/</g,"<").replace(/>/g,">");l[i].setAttribute("href","mailto:"+t.value);}}catch(e){}}}catch(e){}})();
/* ]]> */
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-57962734-1', 'auto');
ga('send', 'pageview');
</script>
<script type="text/javascript">
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};
var pathname = getLocation(window.location.href).pathname;
if(pathname[pathname.length - 1] == '/')
pathname = pathname.substring(0, pathname.length - 1);
function setActive(){
var $this = $(this);
if($this.data('url') && pathname.indexOf($this.data('url')) == 0){
$(this).addClass('active');
}
}
$('ul.chapter-list li')
.each(setActive);
$('ul.section-list li')
.each(setActive);
</script>
</body></html>