Creating Django Migrations

This lesson will show you what steps to take to before making migrations, You’ll learn how to create a new Django project, create models and how to make migrations from models.

00:01 Let’s have a look at creating migrations. To create migrations, you’ll need a Django project. Let’s create one now. I’ve made a new directory and I’m installing Django into a new virtual environment.

00:23 Now the virtual environment will be activated.

00:32 And then I’m going to clear the screen to make it a little easier to see what’s going on. So next up, creating a new project with django-admin.

00:48 You can see the contents of the directory there, and you can see, which we’ll be making a lot of use of throughout this tutorial.

01:05 And there using the tree command, you can see the structure of the files and directories that we have so far. You can notice that at the moment our migrations/ folder inside historical_data/ is empty, so next up it will be time to create the model that we be working with and install the historical_data app into Django.

01:37 First up, you’re adding 'historical_data' to the INSTALLED_APPS in Django’s file. One thing I’ve gotten in the habit of doing is putting a comment to separate out third-party apps and local ones I’ve been working on because later on when things get a little more complicated, it can make it clearer to see what’s happening.

02:04 It’s good practice to put a comma (,) after the last item on any list that you make, because then if you do extend it in the future, you won’t generate any errors.

02:15 Next, creation of the model in the historical_data/ file.

02:27 There you can see the basic model to keep track of Bitcoin prices with three fields in there. We have date, that’s a datetime field. We have price as a decimal field.

02:41 We have volume as an integer field. Now that the model has been created, the next thing you need to do is to make a migration for it, and that can be done using makemigrations.

02:59 You can optionally add the name of the app. This will make migrations solely for the app in question and can simplify debugging later on. Now the migration has been done and if we look again at the structure of our files and directories, we can see that our migrations/ folder contains the migration.

03:25 The eagle-eyed amongst you may also have noticed that a database file has been created. Let’s take a look at that now using dbshell.

03:39 The SQLite instruction to look at the tables is simply .tables, and we can see at the moment there are no tables. .quit puts us back at the prompt.

Avatar image for 2020oracle

2020oracle on July 13, 2021

Rapid fire coding with little or no context.

Avatar image for Martin Breuss

Martin Breuss RP Team on July 13, 2021

Hi @2020oracle, since this is a short course that’s focused on Django Migrations specifically, Darren does a lot of the initial setup without digging into the details.

If you’re just getting started with Django, you might want to check out these courses first:

Both of these go over concepts more slowly and are focused on learners who are just starting out with Django. Hope that helps!

Avatar image for aharrislive

aharrislive on Feb. 16, 2022

Is there a setting in bash you used to show your file before going into the folder that contains it?

Avatar image for nicolae bulat

nicolae bulat on July 31, 2023

Hi! can’t find the “tree” comand in pycharm. It says “zsh: command not found”. any advise? thanks

Avatar image for Darren Jones

Darren Jones RP Team on Aug. 1, 2023

Hi nicolae. It sounds like you’re on macOS, and if so, you need to install tree separately. You can do this with homebrew, so if you already have it installed, then the command is brew install tree and then you should be good to go.

Become a Member to join the conversation.