1 00:00:07,970 --> 00:00:09,540 So, welcome back. 2 00:00:09,540 --> 00:00:12,810 Now what I want to do is I want to dig into a little bit 3 00:00:12,810 --> 00:00:16,530 about how you as a developer of a Django application 4 00:00:16,530 --> 00:00:20,280 interact with Django itself with objects. 5 00:00:20,280 --> 00:00:21,650 So, just to recall, 6 00:00:21,650 --> 00:00:25,430 if you're looking for the downloads, dj4e.com is the place to go. 7 00:00:25,430 --> 00:00:28,760 We are working through the Django tutorial 8 00:00:28,760 --> 00:00:32,180 and I'm pretty much no further than part 2 in this particular one 9 00:00:32,180 --> 00:00:34,410 because it really not worried about building the Django app, 10 00:00:34,410 --> 00:00:37,300 I'm really interested in how objects work. 11 00:00:37,300 --> 00:00:41,225 So, one of the things that you can do because Django is really cool open-source, 12 00:00:41,225 --> 00:00:43,610 is we're actually going to look at the source code to Django. 13 00:00:43,610 --> 00:00:50,195 Django is open-source and has 26,000 different separate modifications, 14 00:00:50,195 --> 00:00:54,560 almost 1,700 contributors, and it's a lot of source code. 15 00:00:54,560 --> 00:00:57,110 It's probably a million lines of code. 16 00:00:57,110 --> 00:00:59,570 And so this is real enterprise software, 17 00:00:59,570 --> 00:01:02,840 it's very exciting stuff and open source 18 00:01:02,840 --> 00:01:05,660 is really cool because we can look inside Django. 19 00:01:05,660 --> 00:01:07,850 Django is not a magical thing that we can't see 20 00:01:07,850 --> 00:01:10,130 and we can learn from it and understand it. 21 00:01:10,130 --> 00:01:11,540 So, what I'm going to try to do, 22 00:01:11,540 --> 00:01:14,360 is I'm going to take you inside Django 23 00:01:14,360 --> 00:01:18,140 and our application that we write is going to be really simple. 24 00:01:18,140 --> 00:01:20,420 It's just pulls application that just prints out, 25 00:01:20,420 --> 00:01:22,790 "Hello World" because this isn't a Django class, 26 00:01:22,790 --> 00:01:26,255 this is really how object-oriented works inside Django. 27 00:01:26,255 --> 00:01:29,210 So, what we're doing here is if you 28 00:01:29,210 --> 00:01:32,779 read the Django documentation and you read the Django tutorial, 29 00:01:32,779 --> 00:01:34,115 what you find is that, 30 00:01:34,115 --> 00:01:38,420 oh okay there's this big thing called Django and you got to write these files, 31 00:01:38,420 --> 00:01:42,080 this file URLs that routes things and models that creates 32 00:01:42,080 --> 00:01:46,400 database tables and allows you to read and write data from those database tables, 33 00:01:46,400 --> 00:01:49,250 and the views that shows what the user interface of this is. 34 00:01:49,250 --> 00:01:52,580 And so, you're supposed to create these files. 35 00:01:52,580 --> 00:01:56,675 So, I'm going to focus mostly on the models file and talk 36 00:01:56,675 --> 00:02:01,250 a little bit about what's going on inside of Django and how Django 37 00:02:01,250 --> 00:02:05,390 in effect creates classes and then you create classes that derive from 38 00:02:05,390 --> 00:02:08,045 the Django classes and then you can create 39 00:02:08,045 --> 00:02:11,765 objects that are based on your classes and then use those objects. 40 00:02:11,765 --> 00:02:15,395 We're going to dig in to the source code to Django. 41 00:02:15,395 --> 00:02:16,820 So, this particular one, 42 00:02:16,820 --> 00:02:19,550 let's take a look at this base.py, 43 00:02:19,550 --> 00:02:23,700 let me show you the models file that's our code. 44 00:02:23,980 --> 00:02:29,200 So, here's our models file and if you read the documentation and in Django it says, 45 00:02:29,200 --> 00:02:31,255 well if you want to create a database table, 46 00:02:31,255 --> 00:02:33,880 it's called a model and the model view controller. 47 00:02:33,880 --> 00:02:37,090 We're going to make a database table, has question_text, 48 00:02:37,090 --> 00:02:40,300 publication date, and then have some methods inside of it, 49 00:02:40,300 --> 00:02:44,140 like how to print this particular object out as a string 50 00:02:44,140 --> 00:02:46,030 and then this was published recently 51 00:02:46,030 --> 00:02:47,530 is something that they're going to use in there. 52 00:02:47,530 --> 00:02:50,080 Then we're going to another table called choice 53 00:02:50,080 --> 00:02:53,725 and it's going to have a question column and a text column. 54 00:02:53,725 --> 00:02:59,200 We can see this, there's this thing called migrations that reads this and creates tables. 55 00:02:59,200 --> 00:03:00,760 And so, in here, 56 00:03:00,760 --> 00:03:05,635 we have polls2_choice_question, 57 00:03:05,635 --> 00:03:13,910 polls2_choice which has those columns and it's created this database table for us. 58 00:03:13,910 --> 00:03:18,920 This thing not only allows us during runtime to read and write from the database, 59 00:03:18,920 --> 00:03:22,040 but it also at setup time it creates things for us. 60 00:03:22,040 --> 00:03:27,440 And so these two database tables that I've got sitting in my database here were created 61 00:03:27,440 --> 00:03:30,290 from this object because it runs one process to read 62 00:03:30,290 --> 00:03:33,170 through your objects then create database tables. 63 00:03:33,170 --> 00:03:36,335 It's another process while it's doing request-response cycles, 64 00:03:36,335 --> 00:03:40,270 then it actually reads and writes the data in the database. 65 00:03:40,270 --> 00:03:45,975 So that's one nice thing and so models.Model is a class. 66 00:03:45,975 --> 00:03:48,300 If you just look at this, we're creating a new class, 67 00:03:48,300 --> 00:03:49,920 that's Question, it's our class 68 00:03:49,920 --> 00:03:52,400 and we're extending the models.Model class 69 00:03:52,400 --> 00:03:56,555 and we're doing this to inherit lots and lots and lots of functionality. 70 00:03:56,555 --> 00:04:02,360 And then, the definition of this is just variables models.Charfield, 71 00:04:02,360 --> 00:04:06,320 that's a variable or constant constant with 72 00:04:06,320 --> 00:04:11,670 some stuff and these are objects and all this stuff is sitting there for us. 73 00:04:12,760 --> 00:04:17,420 It's almost like a dialect of Python 74 00:04:17,420 --> 00:04:21,780 that is specifically to define these database models. 75 00:04:22,170 --> 00:04:28,020 So, if you know that this is a class and that it's extending another class, 76 00:04:28,020 --> 00:04:30,820 let's go find the source code to that class. 77 00:04:30,820 --> 00:04:36,065 So, now what we're going to do is we're going to go into the Python GitHub repository 78 00:04:36,065 --> 00:04:41,960 and here on line 383 in the base.py, 79 00:04:41,960 --> 00:04:45,920 this big long thing if you're looking for it and its linked 80 00:04:45,920 --> 00:04:50,270 here from this slide right here and now this is Django code. 81 00:04:50,270 --> 00:04:52,700 This is the code that was written by the Django project. 82 00:04:52,700 --> 00:04:55,950 And so, they made this model and if you look at this, 83 00:04:55,950 --> 00:04:59,050 you've made simple classes. 84 00:04:59,050 --> 00:05:02,315 This is not that different than the classes you made. 85 00:05:02,315 --> 00:05:06,230 So, you have _init_ method and the first parameter is self. 86 00:05:06,230 --> 00:05:09,784 Now that should look familiar to you and then you have a couple of arguments, 87 00:05:09,784 --> 00:05:11,870 and then we got a whole bunch of code. 88 00:05:11,870 --> 00:05:13,840 And so, if we start looking down here, 89 00:05:13,840 --> 00:05:16,120 this is just the constructor. 90 00:05:16,120 --> 00:05:20,990 So, part of the goal of object-oriented programming is you get to write 91 00:05:20,990 --> 00:05:23,150 a little bit of code that leverages 92 00:05:23,150 --> 00:05:26,045 hundreds and hundreds of lines of code that somebody else, 93 00:05:26,045 --> 00:05:29,405 some other really sharp people, 94 00:05:29,405 --> 00:05:31,730 let's see if we can find the contributors. 95 00:05:31,730 --> 00:05:34,400 So, this is a list of all the people that built this. 96 00:05:34,400 --> 00:05:37,370 These people know a lot, 97 00:05:37,370 --> 00:05:39,050 there's a whole bunch of people and we can 98 00:05:39,050 --> 00:05:42,260 see their work and what they're doing and how long they've been 99 00:05:42,260 --> 00:05:47,370 working and they're doing things like reading all these documents, 100 00:05:47,370 --> 00:05:49,340 like the hypertext transfer protocol 101 00:05:49,340 --> 00:05:51,919 and this and that and knowing how to talk to databases, 102 00:05:51,919 --> 00:05:54,380 and how to talk to SQLite database and how to 103 00:05:54,380 --> 00:05:57,350 talk to MySQL database and Postgres database and Oracle database. 104 00:05:57,350 --> 00:05:59,390 You literally don't need to know anything. 105 00:05:59,390 --> 00:06:01,325 And so what they're doing, is they're building 106 00:06:01,325 --> 00:06:04,850 a really large object or 107 00:06:04,850 --> 00:06:08,420 class that then you can extend and you don't have to write all this code. 108 00:06:08,420 --> 00:06:10,160 This is very general-purpose code. 109 00:06:10,160 --> 00:06:14,500 Part of the goal of writing an object is to hide this from you. 110 00:06:14,500 --> 00:06:16,065 Now, we can look at it, 111 00:06:16,065 --> 00:06:17,900 you shouldn't have to look at it. 112 00:06:17,900 --> 00:06:19,715 You should be able to look at 113 00:06:19,715 --> 00:06:24,860 the documentation i.e the documentation on these little tutorials. 114 00:06:24,860 --> 00:06:27,665 I'm just trying to give you the sense that, 115 00:06:27,665 --> 00:06:29,780 here we go and so this is still the constructor. 116 00:06:29,780 --> 00:06:32,980 This is the initialization that constructor, 117 00:06:32,980 --> 00:06:36,680 so now we now have a method and so these are 118 00:06:36,680 --> 00:06:40,220 other methods and these are things that you'll recognize, 119 00:06:40,220 --> 00:06:43,580 the double underscore string underscore that's what happens when we 120 00:06:43,580 --> 00:06:48,770 convert a model object to a string underscore underscore. 121 00:06:48,770 --> 00:06:52,035 So, this is no different than if you convert a dictionary to a string. 122 00:06:52,035 --> 00:06:53,360 There's a little method inside of 123 00:06:53,360 --> 00:06:56,465 Python that's double underscore string double underscore. 124 00:06:56,465 --> 00:06:58,940 eq, that's comparing equality. 125 00:06:58,940 --> 00:07:02,420 Hashing has to do with how dictionaries work, getstate, 126 00:07:02,420 --> 00:07:08,900 setstate and all these things are just the things that if I am the Django developer, 127 00:07:08,900 --> 00:07:14,060 I have to do this because that lets me mount my class into Django the 128 00:07:14,060 --> 00:07:20,202 right way and these are methods that would be documented like serializable value- 129 00:07:20,202 --> 00:07:25,505 Save, and there's another like whole bunch of stuff. 130 00:07:25,505 --> 00:07:28,309 See, how much work it save base. 131 00:07:28,309 --> 00:07:31,750 If you're probably looking at the Django documentation to see all this stuff. 132 00:07:31,750 --> 00:07:37,320 Save parents, were still in the model object. 133 00:07:37,320 --> 00:07:42,105 Right? Next to prepare database save, clean, validate, 134 00:07:42,105 --> 00:07:48,005 unique, and so this is just code that was written by these people. 135 00:07:48,005 --> 00:07:49,675 Where are they? All right. 136 00:07:49,675 --> 00:07:52,040 All these people wrote all this code, 137 00:07:52,040 --> 00:07:55,645 and read all the documentation and tested the heck out of it. 138 00:07:55,645 --> 00:08:00,915 You just write this code right here. 139 00:08:00,915 --> 00:08:04,200 You extend, and you inherit all that stuff. 140 00:08:04,200 --> 00:08:09,255 That does things like creates other tables which I showed you. 141 00:08:09,255 --> 00:08:13,125 It actually hooks into an administrator interface. 142 00:08:13,125 --> 00:08:15,195 Let me see if I can show you that. 143 00:08:15,195 --> 00:08:17,735 This is an administration interface. 144 00:08:17,735 --> 00:08:24,635 So, this user interface is part of your Django application that came from Django, 145 00:08:24,635 --> 00:08:26,165 that's the admin tool, 146 00:08:26,165 --> 00:08:28,370 the admin panel within Django, 147 00:08:28,370 --> 00:08:31,895 and your model is now in there. 148 00:08:31,895 --> 00:08:33,155 All right? 149 00:08:33,155 --> 00:08:37,180 So, you got some question text and date published et cetera. 150 00:08:37,180 --> 00:08:43,390 So, all you did was created one little file. 151 00:08:43,390 --> 00:08:46,265 Right? Django is this magical thing, 152 00:08:46,265 --> 00:08:48,689 administrator interface, database creation, 153 00:08:48,689 --> 00:08:50,680 table creation, et cetera. 154 00:08:50,680 --> 00:08:54,400 You created this little file, and away you go. 155 00:08:54,400 --> 00:08:58,030 Okay? So, then I'll show you one more thing. 156 00:08:58,030 --> 00:09:00,770 Just the pattern you'll see it's kind of the same. 157 00:09:00,770 --> 00:09:03,585 Is in this views.py file. 158 00:09:06,205 --> 00:09:12,980 So, this is returning a response when you go here into the polls to application. 159 00:09:12,980 --> 00:09:14,900 Right? It returns this response. 160 00:09:14,900 --> 00:09:21,945 But if you were to look at this in the developer console under network, 161 00:09:21,995 --> 00:09:27,785 you would see that there is a whole bunch of stuff that you going to do to do HTTP. 162 00:09:27,785 --> 00:09:30,465 We sort of saw that in the first video. 163 00:09:30,465 --> 00:09:31,980 Like the response headers, 164 00:09:31,980 --> 00:09:33,800 all these things what the content is, 165 00:09:33,800 --> 00:09:36,435 what the type is, the date 166 00:09:36,435 --> 00:09:39,525 these things and there's many other things that might be necessary. 167 00:09:39,525 --> 00:09:42,185 So, you could learn how to write all these things, 168 00:09:42,185 --> 00:09:44,805 or you could basically say, you know what? 169 00:09:44,805 --> 00:09:47,420 I'm just going to inherit the obvious things, 170 00:09:47,420 --> 00:09:51,855 and I'm going to read the documentation on HttpResponse. 171 00:09:51,855 --> 00:09:53,290 So, this is an object, 172 00:09:53,290 --> 00:09:55,175 this is the constructor to that object, 173 00:09:55,175 --> 00:09:57,655 and we're going to send in a string to the constructor. 174 00:09:57,655 --> 00:09:59,775 So, with HttpResponse we could create it, 175 00:09:59,775 --> 00:10:01,065 we could set headers, 176 00:10:01,065 --> 00:10:02,755 we can do all kinds of things in it. 177 00:10:02,755 --> 00:10:08,350 Not just the text, but all kinds of other things that have to do with the HTTP protocol. 178 00:10:08,350 --> 00:10:10,875 So, somewhere inside of Django, 179 00:10:10,875 --> 00:10:13,845 they have defined this object that's the HttpResponse. 180 00:10:13,845 --> 00:10:16,060 So, I can show us that one as well. 181 00:10:16,060 --> 00:10:18,615 Take a look at the HttpResponse. 182 00:10:18,615 --> 00:10:20,850 We're using it in a very simple way. 183 00:10:20,850 --> 00:10:23,710 But, if you look inside Django, 184 00:10:23,710 --> 00:10:25,795 you see there's a lot to it. 185 00:10:25,795 --> 00:10:28,305 So, here we go is content, 186 00:10:28,305 --> 00:10:31,385 is a set of bytes in this case, 187 00:10:31,435 --> 00:10:35,040 and that's the constructor, 188 00:10:35,040 --> 00:10:39,280 then there's serialization et cetera. 189 00:10:39,280 --> 00:10:46,615 So, all those things are part of that object and HttpResponseBase, 190 00:10:46,615 --> 00:10:50,135 and we'd go find that one which I think is just up here. 191 00:10:55,575 --> 00:11:03,020 So, even within this file they're using one class HttpResponseBase, 192 00:11:03,020 --> 00:11:07,905 content-type, that's the thing about its text/HTML_status, 193 00:11:07,905 --> 00:11:12,005 that was the 200 okay et cetera charset UTF8. 194 00:11:12,005 --> 00:11:14,755 So, we're setting that thing up that's a constructor here. 195 00:11:14,755 --> 00:11:18,295 If you were looking at the documentation, you will see that, 196 00:11:19,675 --> 00:11:27,775 the charset, setting a cookie, 197 00:11:27,775 --> 00:11:30,185 cookie has to do with the little bit of 198 00:11:30,185 --> 00:11:33,149 data that goes back and forth between the server and the browser. 199 00:11:33,149 --> 00:11:34,665 You don't have to know how to do that. 200 00:11:34,665 --> 00:11:38,820 You can go read that. You go into RFC 2616. 201 00:11:38,820 --> 00:11:42,465 If I got that, 202 00:11:42,465 --> 00:11:48,905 look at 2616, and then we could search for cookie. 203 00:11:49,935 --> 00:11:57,425 How come I don't see that. 204 00:11:57,425 --> 00:12:01,420 RFC HTTP cookies, which one is it? 205 00:12:01,420 --> 00:12:03,390 Maybe it's got its own document. 206 00:12:03,390 --> 00:12:06,365 Yeah, there we go. Rfc 6265. 207 00:12:06,365 --> 00:12:09,680 So, if you want to set a cookie which is how we log in and out of applications. 208 00:12:09,680 --> 00:12:13,455 You've to go read this one. How many pages is this? 209 00:12:15,525 --> 00:12:18,065 It's only 37 pages. 210 00:12:18,065 --> 00:12:24,425 But it turns out, that thankfully somebody in the Django project, where are we? 211 00:12:25,175 --> 00:12:28,655 As a little command, 212 00:12:29,485 --> 00:12:32,365 has a method called set-cookie for us, 213 00:12:32,365 --> 00:12:34,250 and for you to go look at the documentation. 214 00:12:34,250 --> 00:12:37,500 If this HttpResponse want to Set-Cookie back in the browser, 215 00:12:37,500 --> 00:12:39,245 instead of reading all this stuff, 216 00:12:39,245 --> 00:12:43,870 some kind person who helped write Django wrote all this down, 217 00:12:43,870 --> 00:12:46,470 and gave you a method called set-cookie. 218 00:12:46,470 --> 00:12:49,395 So, I'll stop there, 219 00:12:49,395 --> 00:12:53,225 I'm not really trying to get you to sort of be a wizard in any of this stuff, 220 00:12:53,225 --> 00:12:57,470 I'm trying to give you a little tiny places where you can jump in and learn more. 221 00:12:57,470 --> 00:13:02,565 The basic idea is there's lots of complexity to this, lots of documentation, 222 00:13:02,565 --> 00:13:05,120 lots of rules about how to comply to this stub, 223 00:13:05,120 --> 00:13:07,180 and Django has made it, 224 00:13:07,180 --> 00:13:09,775 so that's all hidden behind this abstraction boundary. 225 00:13:09,775 --> 00:13:12,715 You have objects, you make those objects. 226 00:13:12,715 --> 00:13:14,610 So, we played with the models object, 227 00:13:14,610 --> 00:13:17,145 and then we played with the HttpResponse object, 228 00:13:17,145 --> 00:13:19,935 and we can use as much or as little, 229 00:13:19,935 --> 00:13:22,185 we didn't have to use the cookie and the response object. 230 00:13:22,185 --> 00:13:25,590 We can use as much or as little of that stuff as we want. 231 00:13:25,590 --> 00:13:31,685 That's the beauty, is that all that complicated detail is hidden inside 232 00:13:31,685 --> 00:13:38,060 Django with the convenience of we can use as little or as much of it as we want. 233 00:13:38,060 --> 00:13:42,175 So, I hope you didn't try to like write Django because of this. 234 00:13:42,175 --> 00:13:47,825 I just wanted to make a little connection with how you work with large frameworks, 235 00:13:47,825 --> 00:13:50,890 or large libraries, and how object-oriented programming, 236 00:13:50,890 --> 00:13:54,205 and the patterns of abstraction, and hiding, 237 00:13:54,205 --> 00:13:59,005 and inheritance can be put to good use both by building libraries, 238 00:13:59,005 --> 00:14:00,585 and then by using those libraries. 239 00:14:00,585 --> 00:14:03,345 I hope you found it useful. Cheers.