1 00:00:01,260 --> 00:00:08,120 In this video, we are going to learn about models in Django. models is used to save the 2 00:00:08,160 --> 00:00:13,720 database information. Each model maps to a single database table. 3 00:00:13,880 --> 00:00:20,320 Let’s just start coding. Open models.py file inside app1 directory. 4 00:00:22,940 --> 00:00:26,700 Let’s say we want to create a Friends table. 5 00:00:33,860 --> 00:00:37,940 This table will save the name & the age of our friends. 6 00:00:38,480 --> 00:00:40,560 The type of name will be CharFields. When we use CharFields, we need to define max_length. 7 00:00:40,560 --> 00:00:52,520 For the age, we will use IntegerField. 8 00:00:58,900 --> 00:01:07,140 After create a models, we need to makemigrations and migrate our Django project. Open the command prompt 9 00:01:07,480 --> 00:01:17,780 stop the project by use a combination key ctrl+c, and then type python manage.py makemigrations 10 00:01:24,560 --> 00:01:28,180 and then python manage.py migrate. 11 00:01:33,340 --> 00:01:39,600 Okay our database is created successfully. Let’s Start the project again. 12 00:01:44,840 --> 00:01:48,780 Let’s see that our project still working in browser. 13 00:01:55,680 --> 00:01:59,020 Okay, all of page is still working. 14 00:01:59,960 --> 00:02:06,780 Now I will show you about admin page in Django. This is admin page. 15 00:02:06,940 --> 00:02:13,860 In this page, we can manage our database, include a Friends database that we just created. 16 00:02:14,580 --> 00:02:22,380 But we don’t have a username & password yet. Let’s create it first. Open the command prompt 17 00:02:23,300 --> 00:02:29,860 stop the project, and type python manage.py createsuperuser. 18 00:02:35,540 --> 00:02:45,760 Here we can type your username, for email address, you can leave it blank. Type your secure password. 19 00:02:47,080 --> 00:02:53,720 If your password is not secure, you will get a warning like this. In this case, I will 20 00:02:53,720 --> 00:02:57,680 ignore the warning and bypass the password validation. 21 00:02:58,700 --> 00:03:01,860 Okay, now we have a user. 22 00:03:01,860 --> 00:03:04,520 Don’t forget to run the project again. 23 00:03:09,400 --> 00:03:17,740 Now let’s back to the browser and try to login to Django admin page using a user that we just created 24 00:03:20,640 --> 00:03:25,840 Good ! Now we are successful login to Django admin page 25 00:03:26,380 --> 00:03:29,900 but we can’t find Friends database here. 26 00:03:30,920 --> 00:03:37,900 To display friends database to the Django admin page, we need to register the friend database first. 27 00:03:38,800 --> 00:03:48,680 Open visual studio code and go to admin.py file, here we need to register our friends database 28 00:03:49,180 --> 00:03:52,600 Firstly import the Friends from models 29 00:03:56,100 --> 00:03:58,180 and then register it. 30 00:04:14,220 --> 00:04:17,660 Now let’s open Django admin page again. 31 00:04:20,360 --> 00:04:24,680 Here we can see that now we have friends database. 32 00:04:25,100 --> 00:04:28,040 Let’s add some friends to the database. 33 00:04:38,280 --> 00:04:46,480 Okay we have some friends here, but the display will be Friend object 1, 2, 3 like this. 34 00:04:47,060 --> 00:04:52,500 We want to display the name of our friends instead just friend object like this. 35 00:04:52,940 --> 00:04:58,380 Open visual studio code again and go to models.py file 36 00:05:02,480 --> 00:05:05,980 we need to create def str like this 37 00:05:09,640 --> 00:05:11,620 and return the name. 38 00:05:14,280 --> 00:05:17,220 Let’s look at Django admin page again. 39 00:05:22,140 --> 00:05:26,420 Cool ! Now we can see the name of our friends. 40 00:05:28,160 --> 00:05:33,400 This is the end of this video. In the next video, we are going to learn how to display 41 00:05:33,400 --> 00:05:36,400 all of our friend to the hello_world page. 42 00:05:36,400 --> 00:05:39,520 Thankyou for watching and see you on the next video!