In this video, we are going to learn about models in Django. models is used to save the 2
database information. Each model maps to a single database table. 3
Let’s just start coding. Open models.py file inside app1 directory. 4
Let’s say we want to create a Friends table. 5
This table will save the name & the age of our friends. 6
The type of name will be CharFields. When we use CharFields, we need to define max_length. 7
For the age, we will use IntegerField. 8
After create a models, we need to makemigrations and migrate our Django project. Open the command prompt 9
stop the project by use a combination key ctrl+c, and then type python manage.py makemigrations 10
and then python manage.py migrate. 11
Okay our database is created successfully. Let’s Start the project again. 12
Let’s see that our project still working in browser. 13
Okay, all of page is still working. 14
Now I will show you about admin page in Django. This is admin page. 15
In this page, we can manage our database, include a Friends database that we just created. 16
But we don’t have a username & password yet. Let’s create it first. Open the command prompt 17
stop the project, and type python manage.py createsuperuser. 18
Here we can type your username, for email address, you can leave it blank. Type your secure password. 19
If your password is not secure, you will get a warning like this. In this case, I will 20
ignore the warning and bypass the password validation. 21
Okay, now we have a user. 22
Don’t forget to run the project again. 23
Now let’s back to the browser and try to login to Django admin page using a user that we just created 24
Good ! Now we are successful login to Django admin page 25
but we can’t find Friends database here. 26
To display friends database to the Django admin page, we need to register the friend database first. 27
Open visual studio code and go to admin.py file, here we need to register our friends database 28
Firstly import the Friends from models 29
and then register it. 30
Now let’s open Django admin page again. 31
Here we can see that now we have friends database. 32
Let’s add some friends to the database. 33
Okay we have some friends here, but the display will be Friend object 1, 2, 3 like this. 34
We want to display the name of our friends instead just friend object like this. 35
Open visual studio code again and go to models.py file 36
we need to create def str like this 37
and return the name. 38
Let’s look at Django admin page again. 39
Cool ! Now we can see the name of our friends. 40
This is the end of this video. In the next video, we are going to learn how to display 41
all of our friend to the hello_world page. 42
Thankyou for watching and see you on the next video!