Changing Django Models
When your models change, you need to create and apply a migration. In this lesson, you’ll learn how to make changes to your models, apply them to the database using the makemigrations
command, and also get to see how to use the python manage.py dbshell
command to inspect the database..
00:01 Next, let’s look at changing models. As your Django project develops, your models will likely change. Sometimes you’ll add fields or sometimes you’ll change their types and options.
00:13 Whenever you do this, the database tables used to store these models need to be changed too. This is where migrations really come into their own, because it means you won’t need to work out what code is needed to change a field of one type into another within the database. Let’s see this in action.
00:32
Here is the PriceHistory
model. Let’s change volume
to a DecimalField
.
00:45 With that complete, you can now make a new migration using the same command as seen previously. There you can see another migration file has been made. Applying it works in the same way as before.
01:14
dbshell
can be used to take a look at the changes which have been made. And you can see that "volume"
is now a decimal
, not an integer
.
Become a Member to join the conversation.
tsusadivyago on May 11, 2020
right now the table had no data so the migration went smooth, what if data was already present in table