How to View the SQL Code of a Models.py File in Django



Python


In this article, we show how to view the SQL code of a models.py file in Django.

So, if you come from a background in SQL languages such as MySQL or PostgreSQL, you're very familiar with the SQL lingo such as VARCHAR, primary key, autoincrement, and all these various terms.

Working with SqLite3 in Django is somewhat different. Instead of terms such as VARCHAR, SqLite3 uses CharField. Instead of table, SqLite3 uses model.

It's somewhat different, and you really have no choice but to learn the new lingo.

But you can also translate the code from what you create in a models.py file and view the true SQL code.

So, we'll show how to do this in this article.

Let's say, for example, we want to create a table called Students that has 3 columns, firstname, lastname, and email address.

To do this, you would go to the App that you have created. In my case, it's the Articles App and in the models.py file, you would have the following code.


Models.py File





You first have to import models.

Then you create a table named Students, an instance of Model.

You then create the individual columns, firstname, lastname, and email. Each is of type CharField (which the equivalent in MySQL is VARCHAR.

This is all the code that is needed to create a table caleld Students that has firstname, lastname, and email columns.


makemigrations

We simply created the code, yet we haven't made Django aware that we've made these changes to the model.py file that we've created this new table called Students.

Therefore, we let Django know that we've created these changes through the command prompt.

We specify the full path to the project directory that we've created. Since I named my project directory mywebsite, I specify the full path to mywebsite.

After we have the full path, we type in the following line.



If you're on a Windows PC, you may have to change python to py, so the line would be, py manage.py makemigrations Articles.

Remember to change Articles to the App name for which you've created that models.py file.

Once you've run this code, you should see a number before inital.py. This number is important because we use it in the next line of code.


sqlmigrate

After you've made the migrations through the makemigrations statement, then the last thing to do to view the SQL code of the models.py file, is to go back to the command prompt, and type in the following line.



Again, if you're on a Windows PC, you may have to change python to py, so the line would be, py manage.py sqlmigrate Articles 0001.

In place of 0001, put the number that you see before initial.py.

Once, you've run this code, you should see the SQL equivalent of the models.py execution, what was done.

This is shown below.

Django sqlmigrate

Now you are able to view all of the SQL code translated over from the models.py form of what is really done in SQL language.

This is the power of the sqlmigrate command. It's able to show a person the actual SQL language that you would write in such as from a language such as MySQL.

Of course, if you want to apply all changes so that the entire project has the updated models.py file from your app, you then have to write in the command prompt the line, py manage.py migrate.

And that's all you have to do.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...