Hey, welcome back.
In the previous video, we set up a Django project and the Django app.
So we've got the configuration files here in our project directory.
And this is what we see so far on the front end.
Now we want to develop the app.
We want to add code to the app so that we can customize and build what we actually need, which is the
job application form.
How do we do that?
Well, it's common with Django apps to start from the database.
So a bottom up approach, meaning that you start with a database, the back end, you code the python
files and then you code the front end, which is the HTML part and CSS and bootstrap.
So a bottom up approach starting from the database.
So let's create a database module.
You'll see.
I'll explain as we go through this video, I'll explain what a module is.
So let's stop the server first.
So to stop the Django app, you want to go to the terminal where the Django app is running.
So here.
So put your cursor at the end and press control C, You also see it here.
So on your keyboard control.
C And that will stop the server.
Let's create a model to create a database model you want to go to job application so the app directory
and go to models.
Double click that Models.py was created when we ran the Python Manage.py start app command.
So all these files, including Models.py.
And so Django added this lines in here, but we need to delete this comment and create our model.
So a model meaning a class which you can name whatever you like, but I do recommend to name it form.
And this class should inherit from models dot model.
So this was imported here models.
And so this class is a child of the model class, right?
So a model class now is specialized to specify the columns of.
The database table.
Right.
So our database table will have several fields, several columns, as you know.
So this will be the output at the end.
So this is from the previous Flask app that we built in the previous sections.
But the output will be the same.
So we'll have a first name input box here, last name, email, available start date and current occupation.
So when the user enters the data here, they fill in the data they press submit This data will be stored
in a database table.
Right?
So that table should have these columns, first name, last name, email, date and occupation.
So in here we are designing the database.
That's what it is meant by model.
The database model car fields means a character field with a maximum length of 80.
Make a break line there and you want to duplicate this with Ctrl or command D and change it to last
name, which is also the same type of field.
So this field can accept 80 characters in total.
And then we have the email, this can be an email field, so delete that.
This is a specialized field to store email addresses.
Then we have the date.
This is also another specialized field date field.
And lastly, occupation.
This is just another care field.
Modules that care.
Fields.
Max underscore length is again 80.
And there's one more thing we need to add here, and that is a magic str methods underscore underscore
str self as an argument.
I'll explain all this what this method does return f.
Self dot first name.
So Dot lost.
Name.
What is str.
Here?
Well, let me quickly show you that you in a python console.
So let's say we have a class foo, right?
Now if Foo doesn't have a string method, when you create an instance of foo like this foo with lowercase
f and you print that out, you print foo, you don't get any readable output.
But if you have this class foo again and you have a method str and here you can define what this method
will return.
For example, it might return.
Hello, I am foo right.
And then if you create an instance again of this new foo class and you print foo, then you will get
the string representation of that instance.
Right?
So this magic method defines what should be printed when a print function is used.
Or in another case like in this case, this should print out the name of the user or the surname, or
it will display it in the admin interface when we create an admin interface later on.
So Django will will display this class, the instance of this class in the form of its salts, for example,
or another user.
I hope that's clear.
Right.
So now we have a model, but how do we actually apply this model to the database and where is the database?
Well, the database should be somewhere in here in the project directory.
DB dot SQLite three.
Right.
If you're curious to know what's inside that database.
So if we have this table or not, I can actually show you that to you.
Using the DB browser for SQLite program, you can install this program.
You can search on Google DB browser for SQLite if you're curious, or you can just watch me as I demonstrate
you what this database has inside.
So I want to open the database using this button and I want to locate the Django project.
So there we go.
That's DB SQLite three.
Open it.
And now we don't have any tables.
Right?
But we are defining here a form table.
So how comes that we don't have any tables here?
Well, that's because we haven't applied any migrations.
Database migrations yet.
So what we want to do next is to create migrations.
I'll explain what migrations are.
So we want to go to the terminal and type in Python Manage.py make migrations, don't execute it yet.
So what does this command do now?
Well, if you go to the job application directory, you will find a migrations directory.
If you don't have it, don't worry, it will be created when we run this command.
So until this migrations directory I see this init.py file which is empty.
Now when I run this command.
Python manage.py make migrations.
Some python code will be added to this init method.
I'll explain what that python code is.
First, let's run the command, but make sure that in settings settings which is.
Under my side.
So my side is the project in settings.py.
You have job application added already under installed apps just like you see it here.
So job application is the name of our app job underscore application.
So close settings and run.
This commands python manage.py make migrations.
You should see this output in the command line.
Now if you go back to job application migrations, you will see this empty file again and also this
other file initial.py.
So when we executed this command python manage.py make migrations, what happens is that the form class
is translated into python code.
Some other code.
Right now this code here is like the middleman between the form class and the DB database.
Right?
DB dot sqlite three.
So we still haven't touched the db dot sqlite database.
So if I go back to DB browser, we still see no tables here.
So nothing has been changed in the database.
To apply those migrations we need to execute another command.
Python manage.py migrate.
So press enter.
And now if I go back to DB browser, I want to load the database again to make a refresh.
So db sqlite three open.
Now we see 12 tables.
You see.
One of those tables.
So database tables, one of those is job application form.
And if I expand this, we should see.
These fields, the columns that we created in Models.py.
So to wrap it up, what we did is we created this class.
So the design of our database, the blueprint, then we ran make migrations, Python Manage.py make
migrations that generated this file with python code and this python code.
Is then executed when we run python manage.py migrate and that python code alters the database, it
adds all those tables.
Now what are these other tables?
Well, these are the tables that belong to the project, not the app itself.
So job application form is a table that belongs to the app.
But these were created because.
We also have this project, the Django Project.
So these are like tables which contain configurations about the project.
Of course, if I go to browse data and I select the job application form table, there are no entries
here, but these columns, these rows will be populated with data when we create the front end, the
web page where the user can enter the data their entries.
So one last thing is that if at a future point you want to add another field to this table, I don't
recommend you do that now.
So please don't do it now because you still don't have the proper skills to make such changes.
But if later on you want to add a new field to your database table, for example, you could add a city
field here in the form class.
Then you would have to apply Python manage.py make migrations to create a new file here.
So that's what creates a new.py file under the migrations folder.
And then you'd have to run python manage.py migrate to apply those changes to the database table.
Right.
I hope that's clear.
And yeah, that's about creating a database model and applying that model to the actual database.
So thanks for following.
See you in the next one.