How to Create a Database Table In Django



Python


In this article, we show how to create a database table in Django.

We will be using the default SQLite3 that already comes pre-installed with the Django package.

So, in order to create the database table, we go to the models.py file in the app that we have created.

So, let's say, we've created an app called Articles. We go into the models.py file in Articles, to construct this database table.

In this example, we will create a database table Students.

This database table will have the following columns, first name, last name, student email address. We will keep this database to these 3 columns.

So, let's go over the code that goes into this models.py file.

This code is shown below.



So, this is all the code that is needed to create a table named Students with the columns, firstname, lastname, and email.

The first thing we must do is import from django.db models. models is necessary to create any table.

So, the next thing we must do is create the table name. This is done through the line, class Students(models.Model):

A table name is defined through the keyword class followed by the name of the database table. This is an instance of Model, so you must specify models.Model

The next thing we must do is create the columns. We said that we wanted to create the columns, firstname, lastname, and email.

So, we create all of these columns.

We make all of these columns of type CharField and give them a max length in proportion how long we may anticipate they may range.

Once, we have done this, the database table is all set.

We have all the code needed to create this database table.

However, we haven't really executed this code.

So, even though we've created the code and saved the models.py file, we haven't executed the migrations so that this data can be used outside of the models.py file.

So what we have to do after this is go to the command prompt, specify the full path to the project that you have created. The project that I created was the mywebsite project. So, specify the full path to the project directory in the command prompt.

Then type in the following line.



If you are using a windows PC, you may have to change python to py, so that the line entered is, py manage.py makemigrations Articles

After this is done, you should see the following code.

Django makemigrations




So, you should see that Django now knows that you made the following changes to the models.py file. Think of py manage.py makemigrations App_name as a way of letting Django know that you have made changes to the models.py file in the App that you have.

Next, you must execute the following line shown below.



What this line of code does it lets all apps the whole project know that these changes have been made and all the database information is available to the entire directory.

So, these two lines must be executed in order for everything to work.

And this is how you can create a database table in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...