1 00:00:00,410 --> 00:00:03,060 Hello again, in this video we are going to add 2 00:00:03,061 --> 00:00:07,738 some codes into our models.py file. 3 00:00:07,739 --> 00:00:11,812 As I explained to you, a page, a web page 4 00:00:11,813 --> 00:00:16,042 has a model, a view and an HTML template. 5 00:00:16,043 --> 00:00:20,842 So the model is lower in this trio structure. 6 00:00:20,843 --> 00:00:23,578 Therefore, we are going to start with the models. 7 00:00:23,579 --> 00:00:25,292 What we need to create is a 8 00:00:25,293 --> 00:00:29,480 class, to represent a blog post. 9 00:00:31,130 --> 00:00:33,320 So let's call this a Post. 10 00:00:34,970 --> 00:00:38,752 Now, Post is not a simple class. 11 00:00:38,753 --> 00:00:40,320 Post should be a class 12 00:00:40,321 --> 00:00:47,104 which inherits from, models.Model. 13 00:00:47,105 --> 00:00:50,884 So that makes this class a Model class. 14 00:00:50,885 --> 00:00:54,810 So Model is a specific class which is designed 15 00:00:54,811 --> 00:00:59,156 by the Django authors, the developers of Django, and 16 00:00:59,157 --> 00:01:03,966 it's designed to contain fields of data. 17 00:01:03,967 --> 00:01:06,150 [No Audio] 18 00:01:06,151 --> 00:01:07,768 What do I mean by that? 19 00:01:07,769 --> 00:01:11,512 Well, for example, we said that we want a 20 00:01:11,513 --> 00:01:15,586 title field, a web page as a title. 21 00:01:15,587 --> 00:01:20,274 So that title, when the content creator creates 22 00:01:20,275 --> 00:01:23,292 a title, that title will be stored in 23 00:01:23,293 --> 00:01:26,870 the title field, of the Post table. 24 00:01:27,630 --> 00:01:33,552 So I showed you this DB viewer, which is a program 25 00:01:33,553 --> 00:01:37,382 to see data, to view the data of an SQL database. 26 00:01:37,383 --> 00:01:39,456 So the SQL database is this 27 00:01:39,457 --> 00:01:42,500 one in here, db.sqlite3. 28 00:01:42,501 --> 00:01:45,760 And this Post will create a new 29 00:01:46,930 --> 00:01:51,530 table in the, dbsqlite3 database. 30 00:01:51,531 --> 00:01:53,672 Now these are the tables currently. 31 00:01:53,673 --> 00:01:56,696 So there's no Post table here, but you'll see that 32 00:01:56,697 --> 00:02:00,222 one will be created when we complete this table. 33 00:02:00,223 --> 00:02:03,500 So title now is models., 34 00:02:03,670 --> 00:02:05,006 what is a title? 35 00:02:05,007 --> 00:02:08,348 Is it a CharField? 36 00:02:08,349 --> 00:02:12,018 Yes, I think this type would fit a title. 37 00:02:12,019 --> 00:02:16,956 So this is a database field type, and it 38 00:02:16,957 --> 00:02:20,933 gets a max_length attribute, 39 00:02:20,934 --> 00:02:23,100 [No Audio] 40 00:02:23,101 --> 00:02:25,510 let's say 200 characters. 41 00:02:25,511 --> 00:02:28,576 So there will be a text box, that will allow 42 00:02:28,577 --> 00:02:32,590 up to 200 characters, for the title of the blog. 43 00:02:32,600 --> 00:02:36,366 [No Audio] 44 00:02:36,370 --> 00:02:39,380 Then what else do we have? content, 45 00:02:39,381 --> 00:02:42,058 so the body of the blog. 46 00:02:42,059 --> 00:02:45,928 Now here, I wouldn't go with CharField, because that 47 00:02:45,929 --> 00:02:50,456 is designed more for things, for small pieces of text. 48 00:02:50,457 --> 00:02:53,766 This would be more like a text field, 49 00:02:53,767 --> 00:02:55,700 [No Audio] 50 00:02:55,701 --> 00:02:58,010 and you can leave that empty. 51 00:02:58,011 --> 00:02:59,292 What else? 52 00:02:59,293 --> 00:03:04,364 Well, date_created maybe, which would 53 00:03:04,365 --> 00:03:05,966 be models. 54 00:03:05,967 --> 00:03:08,066 [Author Typing] 55 00:03:08,067 --> 00:03:11,232 DateTimeField. 56 00:03:11,233 --> 00:03:14,102 So you see that, this models module 57 00:03:14,103 --> 00:03:17,340 of Django, has all these fields ready. 58 00:03:18,510 --> 00:03:27,299 So a DateTimeField would be auto, auto_now_add=True, 59 00:03:27,300 --> 00:03:32,692 which means that, when the content creator will be creating a 60 00:03:32,693 --> 00:03:35,946 new blog post from the admin interface, 61 00:03:35,947 --> 00:03:39,544 so they will be writing a title, a content, and then 62 00:03:39,545 --> 00:03:42,792 there will be a save button there, where they can press 63 00:03:42,793 --> 00:03:45,720 that save button and the Post will be live. 64 00:03:46,250 --> 00:03:49,282 When they press, when the contents creator presses 65 00:03:49,283 --> 00:03:53,596 that button, this class will be instantiated and 66 00:03:53,597 --> 00:03:57,516 it will generate the current Date and Time. 67 00:03:57,517 --> 00:04:00,656 And that current Date and Time will be 68 00:04:00,657 --> 00:04:05,696 injected into the date_created field of the 69 00:04:05,697 --> 00:04:10,998 Post table, in the db.sqlite3 database. 70 00:04:10,999 --> 00:04:12,938 So the database has several tables, 71 00:04:12,939 --> 00:04:16,714 Post is one of the tables, Post has several fields, 72 00:04:16,715 --> 00:04:18,930 one of the fields is that. 73 00:04:18,931 --> 00:04:21,028 Okay, I hope that makes sense. 74 00:04:21,029 --> 00:04:25,912 Then we have the slug field, which will be 75 00:04:25,913 --> 00:04:30,050 recording, the part of the URL after the domain. 76 00:04:30,710 --> 00:04:35,048 So as we said, if your domain is example.com and your 77 00:04:35,049 --> 00:04:41,810 Blog Post is about, is about dogs, then the slug will be /dogs. 78 00:04:41,811 --> 00:04:47,586 So the complete URL would be, example.com/dogs. 79 00:04:47,587 --> 00:04:50,512 And here the user, the contents creator will 80 00:04:50,513 --> 00:04:53,504 just write dog or wherever they like. 81 00:04:53,505 --> 00:04:56,822 So the contents creator will enter the slug 82 00:04:56,823 --> 00:05:00,468 in the admin interface, just like they do 83 00:05:00,469 --> 00:05:04,930 with title, content and other, input boxes there. 84 00:05:04,931 --> 00:05:09,738 So slug would be models, again .Slugfield. 85 00:05:09,739 --> 00:05:14,186 So there is a specific class that represents 86 00:05:14,187 --> 00:05:18,600 SlugFields in Django, called SlugField. 87 00:05:18,601 --> 00:05:22,104 Let's put a max_length of 200. 88 00:05:22,105 --> 00:05:25,086 Maybe it makes sense, not to have very long slugs. 89 00:05:25,087 --> 00:05:29,410 So this will not allow the content creator to enter slugs 90 00:05:29,411 --> 00:05:31,100 longer than 200 characters. 91 00:05:32,266 --> 00:05:33,906 And it has to be unique. 92 00:05:33,907 --> 00:05:36,360 So unique equals to True. 93 00:05:37,100 --> 00:05:41,846 If the admin, the content creator enters a slag 94 00:05:41,847 --> 00:05:45,280 which has been entered before, that would not allow 95 00:05:45,281 --> 00:05:48,310 the new slug to be created because, you cannot 96 00:05:48,311 --> 00:05:51,594 have two pages with the same URL. 97 00:05:51,595 --> 00:05:53,748 It would mess things up. 98 00:05:53,749 --> 00:05:56,484 Therefore this argument doesn't let 99 00:05:56,485 --> 00:05:59,610 duplicated URLs to be created. 100 00:05:59,611 --> 00:06:01,310 Then we have the author. 101 00:06:01,890 --> 00:06:04,376 So author the variable name 102 00:06:04,377 --> 00:06:10,340 models.ForeignKey this time, why? 103 00:06:10,870 --> 00:06:15,612 Because, we are going to get this value from another 104 00:06:15,613 --> 00:06:20,733 database table, which has already been created by Django 105 00:06:21,166 --> 00:06:23,166 and we can get that from, 106 00:06:24,666 --> 00:06:34,032 django.contrib. auth.models import. 107 00:06:34,033 --> 00:06:38,500 So from that import User, right. 108 00:06:39,133 --> 00:06:41,812 This is a class also you can 109 00:06:41,813 --> 00:06:43,946 see it, when I hover the mouse. 110 00:06:43,947 --> 00:06:46,932 And therefore in the two arguments here 111 00:06:46,933 --> 00:06:48,166 we need User, 112 00:06:48,167 --> 00:06:49,966 [No Audio] 113 00:06:49,967 --> 00:06:51,674 without the parentheses. 114 00:06:51,675 --> 00:06:54,888 So basically a ForeignKey field type 115 00:06:54,889 --> 00:06:58,046 means that, the value of this database 116 00:06:58,047 --> 00:07:02,878 field, will come from another database table. 117 00:07:02,879 --> 00:07:05,454 In this case is a User database table. 118 00:07:05,455 --> 00:07:08,466 And this field will create on the admin 119 00:07:08,467 --> 00:07:12,572 interface, a drop down list, where the content 120 00:07:12,573 --> 00:07:17,618 creator can choose between different user, usernames. 121 00:07:17,619 --> 00:07:19,350 So it happens automatically. 122 00:07:19,351 --> 00:07:23,610 But we want to specify a few parameters on_delete, 123 00:07:24,270 --> 00:07:30,310 the behavior that will happen when the user is deleted. 124 00:07:30,311 --> 00:07:32,090 So this user is deleted 125 00:07:32,091 --> 00:07:34,270 from the original database table. 126 00:07:34,270 --> 00:07:37,090 [Author Typing] 127 00:07:37,091 --> 00:07:41,284 So CASCADE, but you don't need this, so 128 00:07:41,285 --> 00:07:44,180 delete that, and just leave it like that. 129 00:07:44,870 --> 00:07:46,584 Now, what does this mean? 130 00:07:46,585 --> 00:07:50,328 Well, this means that if a User is deleted from the 131 00:07:50,329 --> 00:07:55,090 user database table, the User Posts will also be deleted. 132 00:07:55,850 --> 00:07:57,960 This is what CASCADE does. 133 00:07:59,050 --> 00:08:03,708 I'll delete this comma here, and the last field we 134 00:08:03,709 --> 00:08:07,596 want to add is status, the status of the Post, 135 00:08:07,597 --> 00:08:11,340 which would be equal to models.IntegerField. 136 00:08:12,030 --> 00:08:17,135 On the admin interface, there will be a status dropdown 137 00:08:17,136 --> 00:08:22,058 list, which will contain two options Draft and Publish. 138 00:08:22,059 --> 00:08:26,020 So the content creator could choose to save 139 00:08:26,021 --> 00:08:30,074 that article, either as Draft or as Published. 140 00:08:30,075 --> 00:08:33,448 So this would make it easier for 141 00:08:33,449 --> 00:08:36,792 content creators to organize blogs, because they 142 00:08:36,793 --> 00:08:39,326 could save an article as drafts. 143 00:08:39,327 --> 00:08:43,096 And then in our Django app, we could 144 00:08:43,097 --> 00:08:46,914 make a conditional not to publish such articles 145 00:08:46,915 --> 00:08:50,156 that have the status as Draft, but only 146 00:08:50,157 --> 00:08:52,626 Publish those, who have the status as Published. 147 00:08:52,627 --> 00:08:56,460 So this would be, a good way to 148 00:08:56,461 --> 00:09:01,414 distinguish between the two types of Posts articles. 149 00:09:01,415 --> 00:09:05,833 So we have to make to have choices which is equal to a 150 00:09:05,834 --> 00:09:07,866 [No Audio] 151 00:09:07,867 --> 00:09:10,570 variable, let's say STATUS. 152 00:09:11,490 --> 00:09:13,290 And what is STATUS? 153 00:09:13,291 --> 00:09:15,668 Well, STATUS has to be a 154 00:09:15,669 --> 00:09:17,433 variable, we have to create. 155 00:09:19,033 --> 00:09:22,566 Here, so status equal to a tuple 156 00:09:22,567 --> 00:09:24,470 [No Audio] 157 00:09:24,471 --> 00:09:25,966 of two tuples, 158 00:09:25,967 --> 00:09:29,133 [No Audio] 159 00:09:29,134 --> 00:09:32,260 one would be, let's say 0 160 00:09:32,790 --> 00:09:36,933 Draft, the other one would be, 1 161 00:09:36,934 --> 00:09:41,466 [Author Typing] 162 00:09:41,467 --> 00:09:45,966 Publish, and the default one would be maybe Draft. 163 00:09:46,330 --> 00:09:51,254 So 0o stands for Draft and 1 would be for Publish. 164 00:09:51,255 --> 00:09:54,400 Now the admin, the content creator will see either 165 00:09:54,401 --> 00:09:57,520 Draft or Publish in that drop down list. 166 00:09:57,521 --> 00:10:01,520 And that completes our class Post. 167 00:10:02,210 --> 00:10:06,666 Now make sure you save this file, File 168 00:10:06,667 --> 00:10:09,652 Save or Command+S or CTRL+S. 169 00:10:09,653 --> 00:10:12,160 And then you need to do something here. 170 00:10:12,790 --> 00:10:18,798 We need to apply that model to our database. 171 00:10:18,799 --> 00:10:21,518 So we have to run some SQL queries. 172 00:10:21,519 --> 00:10:23,592 But as I have mentioned before, you 173 00:10:23,593 --> 00:10:27,202 don't need to run SQL queries explicitly. 174 00:10:27,203 --> 00:10:28,716 All you have to do is run 175 00:10:28,717 --> 00:10:34,230 two commands python manage.py makemigrations. 176 00:10:35,450 --> 00:10:39,088 So currently, if you have a look at the 177 00:10:39,089 --> 00:10:42,752 DB Browser program where we see the data, we 178 00:10:42,753 --> 00:10:47,552 don't have any tables called Posts for now, even 179 00:10:47,553 --> 00:10:52,746 if I refresh this, refresh, there's no such tables. 180 00:10:52,747 --> 00:10:54,148 But now see what's going to 181 00:10:54,149 --> 00:10:56,030 happen, if I run this command. 182 00:10:57,730 --> 00:11:00,324 Okay, something happens here. 183 00:11:00,325 --> 00:11:03,288 Migrations were applied, so the 184 00:11:03,289 --> 00:11:05,534 database structure was changed. 185 00:11:05,535 --> 00:11:07,928 And then we need to add another command which 186 00:11:07,929 --> 00:11:12,820 is that's python manage.py, migrate this time. 187 00:11:14,310 --> 00:11:17,756 So that one and that one will 188 00:11:17,757 --> 00:11:22,332 apply, SQL queries corresponding to this model. 189 00:11:22,333 --> 00:11:26,066 Now, if I go to here refresh, 190 00:11:26,067 --> 00:11:28,300 [No Audio] 191 00:11:28,301 --> 00:11:33,550 you will see this table has been added blog_post. 192 00:11:33,551 --> 00:11:37,920 So the name of this table was constructed using 193 00:11:37,921 --> 00:11:41,722 the app name, which is blog and underscore. 194 00:11:41,723 --> 00:11:45,812 So Django does that automatically and then after the 195 00:11:45,813 --> 00:11:49,440 _post, which is the name of the model. 196 00:11:50,370 --> 00:11:55,599 So if I select that table, you see now that, 197 00:11:57,066 --> 00:12:00,990 we have these, database fields. 198 00:12:01,810 --> 00:12:04,938 So here we see all the fields that we created, 199 00:12:04,939 --> 00:12:07,692 id a field that is created automatically, but we have 200 00:12:07,693 --> 00:12:12,360 title, content, date_created, slug, status, and author_id, right. 201 00:12:13,450 --> 00:12:17,400 That completes the creation of our model. 202 00:12:18,010 --> 00:12:20,988 Now we need to create a view. 203 00:12:20,989 --> 00:12:22,433 Let's do that in the next video.