7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Migrations and Administration

After you have created your models, all you have are a couple of classes in a Python file. In order to actually get these models transformed into database tables, we need to create and execute migrations. I'll show you how to create these migrations, and then I'll show you how to view your new database tables as an administrator.

2.2 Migrations and Administration

So now that we have our models created what we're looking to do is reach an end that's twofold. We want to take these models that we've created and we want to get them represented within our database. So we want to take these models and kind of transform them into tables into our database, into our schema if you will, and then ultimately we want to be able to see the representation of them there kind of from an administrative perspective. So let's see how we're gonna wanna do that. Well the first thing that we want to do is we want to open up our terminal again and we want to go into our news and directory. And what we want to do is we want to create what is known as migrations. They're going to represent our brand new models and really, all a migration is is just a code representation of how we're going to take these models and transform them into tables within our database. So it's quite simple, really. We're gonna start off by using Python again, and then we're going to use manage.py and then we're going to issue a command called make migrations, and then we need to specify what app within our project do we want to create these migrations for, and I wanna create them for the news app. So I'm gonna hit enter and, as you can see here, it's going to complete successfully, and it's going to say that it ran migrations for news. And then it created initial.py and we'll take a look at that in just a second. The interesting thing here is that it creates these things sequentially for you, so this is going to be number one. And as you can see, we've created the model Article, we've created the model Feed, and we've also added the field feed to article. So that's gonna be that glue that's gonna tie those two models together that foreign key relationship. So if we were to go back into our text editor, I can take a look at my migrations folder and as you can see here it created this file for us, this 0001_ initial and in here you're gonna see that it is going to create a couple different operations It's going to create two models. One for article and one for feed, and then it's going to add a feed to represent that foreign key relationship into our database into that article table. So that's pretty nice here. So we've got this going for us but we still want to be able to get at this. We still want to be able to see this. So if I were to open my terminal again and go ahead and start my server, so I'll go ahead and issue the "run server" command. If I were to start this up again and then open up my browser I could then go to my and we'll see that our page is still working there. But one interesting thing here is if I were to add on to that a /admin, you're going to see that this will take us to an administration page, now this is going to be good but bad at the same time. So we see there that by default, we're given this administration capability from Django. But right now we don't have a user, that we could log into this site with. And ultimately, what are we gonna get at through this administration page. Let's tackle those things. One piece at a time. So the first thing that we're going to need to do is we're going to come in here and we're going to stop our server and we'll clear this out, and what I need to do is I need to create a super user. So I'm going to start off by running another command through manage.py and this is going to be called create super user. So what this is going to do is this is going to walk us through the process of creating a superuser that we can log into that administration page with. So I'm gonna give this a user name. And I'm just going to give this email@email.com. And then we're going to specify a password here. And we're going to enter it in again. And now we've had, this super user has been created successfully. So just to see that this worked, let's go ahead and run our server again. Now if I come back in here. I'll just go ahead and refresh the page. So now I believe what I should be able to do, is, I should be able to come in here. And enter in my credentials. And now I can log in. So what I can see now is the site administration page but I'm not really getting anything too useful as of yet. I can see that I can manage the groups. So right now there are no groups. I can add a group if I want. So this is all authentication for you. And I can come into users, and I can see that I have one user account for myself, and that's great, but I'm still not seeing anything that has to do with my models. Not seeing anything to do with feeds or with articles, but we're gonna fix that right now. So let's go ahead and switch back over to out terminal. And let's clear this out. So the next thing I want to do. Is I want to head into my text editor and I wanna come back into my app folder here. So I'm in news and I wanna come into the admin.py file. So now, within here what I can do is I can begin to. Register these models to be available for me to mess around with or insert data or kinda play with from an administration perspective on that administrative dashboard. So the first thing that I'm gonna need to do is I'm going to need to import from my models that I've been creating, I want to import both feed and article. Just like that so once I've done this now I can actually what's known as register these models to make them accessible from the admins site and by doing this or to do this I simply issue the command admin.site.register, and then I specify the model. So I'm gonna register feed, and I'm also gonna register Article. Just like that, very simple. So now just so by doing those simple lines here, I can come back in here. I'm going to run my server again. Get that up and running. And I'm gonna come back into my browser, and I'll go ahead and refresh this now. And all of the sudden we see that, under our news category here, which just so happens to be our news app, I now have. Two different things that I can manage. I can manage Feeds. So I can come in here and take a look at that. There's no feeds to begin with. And I can also manage articles, and there are none there as well. So I'm gonna start off by coming in and adding a Feed. So I can come into my Feeds, and I can come in and select Add feed. And now you're gonna see a representation of the fields that exist, within the feeds table, within our database. So these are gonna look very familiar to you. I have a title, I have a URL, and I have a check box to represent that Boolean value. So I could just start to add things in here, just as I would want to. I could say My Blog. And I could say www.myblogiscool.com or whatever that url happens to be, and then I can go ahead and either choose save and add another, save and continue editing, or just select save. So now I have a single feed object which is gonna be called my blog and I can see what the values are, I can modify things if I want to. I can change the is active, I can delete these records, and I can make modifications and even see the history of changes That I've been making. So now you're starting to see the different things that we can do simply by creating these models and then creating their associated migrations. So just out of curiosity let's come back in here and take a look at articles as well. If I were to select add here we're going to see that I have a number of other options here. So just by the fact that we have selected that we want to have a foreign key relationship between article and feed. I now have a drop down box here, and I can select one of those feed objects. Now I can also add a new one from here, or make changes. And then I can specify the title, URL, description, publication date and time. All those nice things that were actually going to be extracting from these feeds as we go and get them, but we'll get to that point. So I think for now this should be good enough for you to feel fairly comfortable with creating the basic models, and then creating migrations for them, and being able to administrate them, or administer at some level through the administration page of your new site.

Back to the top