Django Tutorial #7 - Migrations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey gang welcome to your seventh django tutorial and in this video we're going to talk about migrations alright so in the last lesson what we did was create a model in the code which was for an article and we had a couple more fields in this but the general concept is the same we created that in the code and the reason we're creating that model is so that we can map that model to a database right so we can store data we can create instances of the model then save that in the database and then retrieve them later on that's what models are all about but so far this is just in our code and our database doesn't really know about the model it doesn't know to create an article table and start storing articles in there who have not made that connection yet so that's what migrations are all about when we make a model what we have to do is migrate that model to the database okay and then the database knows to create a table called articles and store these different columns in it alright title and body and whatever else we have so that's what we're going to do now we're going to take our model and we're going to make a migrations file and then migrate that to the database so speaking of migrations you might have noticed that when we run a server we get this message over here and he says you have 13 unapplied migrations now these are to do with the built-in models that Django has already created for us when we start a project let's do with the admin section authentication uses that kind of thing ok so the first thing we're going to do before we start migrating our own model is to migrate these so I'm just going to exit out of the server by doing ctrl C and the way we can do this the way we can migrate these is by seeing Python manage top py migrate all right so let's do that first of all Python manage dot py and then it's migrated okay so this is gonna sort all that out for us make those migrations so the database knows okay we're going to be storing users and different things that Django wants us to do okay but that was everything jangle did for us we didn't create those models now what we want to do is migrate our role model this article model to the database as well now before we just run migrate we have to actually make a migration file okay and then we migrate the changes so a migration file basically tracks any changes that you make to a model so we're creating this model to begin with like this will make your migrations file then make migrate that to the database but later on down the line we might be making changes to this model and adding in more fields so then what we do is we'd make another migrations file and migrate that to the database so that Django can look at the differences between the migration files and it can update the database basically okay so let's create the migration file first of all we'll go to the console and the way we do that is by saying python managed dot py make my rations okay so press enter that's going to make our migrations file you can see right here it's made one so let's just have a look at that they stored in this migrations folder inside the articles app because that's what we're migrating and right here is where it's created that migrations file so it's basically putting down here exactly what fields we want inside this model and you'll see that it's automatically added that ID field I told you about Django adds the ID field for us all right as well as our fields so now it knows that these are all the fields we want in our table so when we run Python Manish py migrate is good mirror this to a database table called articles okay so let us do that again will say manage py rather Python managed py migrate and that's gonna make this migration its gonna run this migration so that's done now okay so now we've mirrored our model in a database and every time that we make a model or we make a change to a model we're gonna have to do this same two steps we're gonna have to run migrations or make migrations rather then we're gonna have to migrate them okay so the two commands are Python managed py make migrations that makes the migration files and then it's Python manage py migrate not migrates the changes to the database okay so every time we make a model or make a change to a model this is generally what we're gonna be doing all right so that's migration for you now we have our model sorted and migrated it to the database in the next video what we're gonna do is take a look at the jungle or RM and start interacting with that database
Info
Channel: Net Ninja
Views: 103,755
Rating: undefined out of 5
Keywords: django, django tutorial, django tutorials, tutorial, django or beginners, django tutorial for beginners, django blog, django 2, django 1.11, django lessons, blog, migrate, migrations, makemigrations, django migrate, django migrations, migrations file, migrate model, what are migrations
Id: aOLrEkpGWDg
Channel Id: undefined
Length: 5min 18sec (318 seconds)
Published: Wed Nov 22 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.