Hello guys, Welcome to learn vern.
In the last topic, we learned how to work with the django admin panel, where I told you how to set up the django admin panel, how to launch with ‘create super user’ and login. okay?
Today we will see how to add models in django, that is, in the admin panel.
First, Let us understand what are models and why are they created in django?
Right?
So django models represent your tables which are in the database. Right?
In this, your particular class is created and the name of the same particular class is taken as table name.
And whatever variables you pass in that particular class are taken as names of the fields.
right?
If you wish to define the models, then you need to create your models inside a file which is named as Models.py present in the ‘apps’.
How will you do that? Let's see what you will need to do to create.
Here, I have given a structure.
You need to take a class, write the name of the model as “Models.model”.
Now this “models.model” which is written,
In that the first “models” shows you that it is a model of django.okay?
The second “dot model” shows you that the name of your class is the name of your particular table. okay?
In which you need to fire the name of the variable, then models.datatype and its attributes.
I will teach you practically How it is created,
So that you can understand properly.
Right? Clear?
So first of all we will go to our VS code. right?
You need to follow the same steps again.
Creating a project.. You need to do all those things. okay?
So how to do it? Let's do it together.
If you have already made a project, and you wish to work in same project,
So what you need to do is and how you need to do is, I will let you know.
This is my django admin panel in which we created a project in the last video.
You need to click here and open CMD.
So Your cmd is opened. ok?
Here, first of all you need to activate the virtual environment. okay?
And then you need to open your VS code.
Right? So your VS code is opened.
Now as I told you to create your models in models.py,
So here is my app and in this, here is your models.py.
See, django asks us in the comments to create our models here.
So here is a ‘package import’ which is from ‘django.db import models’.
Which means this is a model file of django.
That means whatever classes you create here, will be represented by a table.
So how to create them?
First you need to take a class here.
You can give any name to your class.
Like suppose here I am taking the name “teacher” okay?
then here you need to take “models.model” okay?
Remember that your first part, “models” represents django db.
Which means this class is representing django db.
“model” represents your class, that means this model tells you that the name of your class is the name of your table.
Okay?
Then here you need to take attributes, like I need the first name of the teacher.
Right? And the field which we need to keep is the character field.
Character field means it will include your characters.
Do you remember ‘Var - care’?
At the time of creating maya scale we had given the first name ""var-care"".
we will keep its length as 50. okay?
Similarly I will take another variable which is the ‘last name’ right?
Here, the character field.
Here also I will keep the max length as 50.
Then email id is needed here.
So for email id, we are given an email field okay?
Here also we will keep the max length as 50 right?
I will keep the ‘contact’ in the end .
We will take our contact in the character field.
Character field means your
var - care field okay?
Where you can take anything like, particular integer Values and even particular character values. okay? Right?
So what did we do? We created a model.
Hope you are able to understand me
Model.
“model created” right? Clear?
What is this? This is our model.
Which means a table will be created in a database named “teacher” right?
Now what we need to do next is we need to make a migration.
Meaning after creating a model in django, you have to fire this command in CMD.
Which means to make migration is mandatory.
Why make migration? Because we have created a model.
So first you will need to create that model, Which means you have to make migration of django.
Why? Because whenever you create any ‘user defined models’ in django, then your migration is compulsory because without that django will not show your table.
And what you need to do next is, after making migration, do migrate.
So We need to fire that migration command okay?
How to do it? Let's do it practically okay? I will show you inside the cmd itself.
You need to open CMD.
Here you need to write “python manage.py makemigrations” okay?
Adding “s” is compulsory.
As you will click enter, see this, it performed a migration for an application, in which it created a model named ‘create model teacher’ which means it fired a command. right? Clear?
Now what we need to do is migrate.
Don't forget migrating is compulsory, or else it wont show your table. okay?
So you need to migrate.
Here migration is done.
So as you made migration, see here, a file is created of migrations.
In that, it fired a query, it displayed all the fields like this one portrays that this ‘big auto field’ will create an id field automatically.
character field, email field and its length.
everything is presented okay? Right? Clear?
Now make migration and migration is done.
Now what we need to do is, add models in the django admin panel.
Hope you are following me
Now we will run the admin panel, to do so, we need to run our server okay?
We will open our browser .
Here slash admin. okay?
So we are logged in.
Here, we are not able to see any module named ‘teacher’.
I need to display the table that we created with the name teacher.
So how will that happen?
What you need to do here is, you will have a file named admin.py in your application.
Inside that, you need to write this particular line.
“admin.site.register” and then your model name in brackets.
But what does admin.site.register do?
it registers your models inside the site of your admin.
It has created a method in this way.
you need to register your models inside the site of your admin.
And you can only register through this particular line.
How to do it?
Let's go to the practical part.
Here I opened VS code.
See i have this admin.py in my apps.
Here django says “register your models here”.
First of all I need to import the models.
How to do it? From dot models import star * okay?
Once you start, all the models that you will create in models.py will automatically get access here.
Now what you need to write is admin.site.register okay?
And here is the name of the model.
So what is the name of our model? Teacher.
So here it will be “teacher”.
You just need to do this much.
You need to write only this line.
Here as you refresh this, see we got it.
And as you will click on add, now you can add your teachers here.
Like I added here Sunit Jha.
S@gmail.com and I wrote here his number and saved it.
So see it creates an object.
What did it create? Teacher object one.
As I click on this, it will give me all the details okay? Right?
Here teacher object 1 is written, it means the name of the teacher is not displayed yet.
So what do you need to do?
why is it showing in this way because django is form based. okay?
So obviously it will create an object right?
It will convert the saved data into an object, that means, the data we saved right now has become an object.
Now if i want to show the name of an object how will i show it?
See, you need to go inside the model, here is a predefined function, you need to write def str.
Okay? right? And here what you need to do is, write id self.firstname.
You need to show your first name, so write here first name.
What does this function do? This converts your object into string.
This function is used for converting object into string
okay? Right? Clear? Understood?
Now as you will open admin panel again, refresh it and see here is the name. okay?
Now as we will add particular teachers, like I added rahul sharma, his email id and his number. okay?
I saved, see here “Rahul” is also created.
What did this particular thing happen?
We made models and added in django admin panels and now we can do anything with it.
Admin panel can access anything with these particular panels. right? Clear? Understood?
So when we meet next time, our next video will be on crud operation with django.
Where I will teach you how to integrate templates, how to work with html and how to work with forms.
If you have any queries or comments, click the discussion button below the video and post. there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.