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.
Creating Django Migrations
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.
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
You can see the contents of the directory there, and you can see
manage.py, which we’ll be making a lot of use of throughout this tutorial.
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.
First up, you’re adding
'historical_data' to the
INSTALLED_APPS in Django’s
settings.py 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.
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.
Next, creation of the model in the
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.
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
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.
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
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.
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!
Is there a setting in bash you used to show your manage.py file before going into the folder that contains it?
Become a Member to join the conversation.
2020oracle on July 13, 2021
Rapid fire coding with little or no context.