What are Models?7:14 with Kenneth Love
Django's ORM, like many others, uses classes to define a database table, and that class's attributes to define the columns in the table. We call these classes "models".
django.db.models has most of the model functionality you'll use to create models and their fields.
CharField holds strings.
TextField holds an unspecified amount of text.
More Django model field types.
python manage.py makemigrations [app] will make the migrations for a specific app.
python manage.py migrate [app] will run the pending migrations for a specific app. If you leave off the app name, any pending migrations for any apps will be run.
Here is the list of
[SOUND] If you've done the databases in Python, or 0:00 the flask social network course, you've heard me talk about models before or 0:03 maybe you've used models in a different framework, language, or ORM. 0:09 In Django's ORM, or Object Relational Mapper, 0:14 models are classes that represent database tables. 0:17 Each model is its own table, and 0:21 each attribute on the class is a column on the table. 0:23 When we add new instances of a class to the database, 0:26 the ORM creates a new row in the table. 0:29 Then we can select one or more rows from the table and update or delete them. 0:32 Django's RM offers us many more features than that. 0:36 But, we won't need all of them for this course. 0:39 Let's just worry about creating a fairly simple model, and 0:41 getting it live on our project. 0:44 Models belong to an app, and live in the app's models.py. 0:46 So, let's go ahead and open up that file. 0:49 It's pretty empty to start with but Django went ahead and imported the module 0:54 that has all of the model functionality in it for us, so thanks Django. 0:58 Model numbers should be singular by convention and 1:03 quite often you're dealing with a single model instance. 1:07 So it's weird to say I have this one apples. 1:10 Our model is going to be named Course because we're talking about courses. 1:13 And as it is a class it should start with a capital letter. 1:17 And it's going to inherit from models.Model because it is a model. 1:22 Let's fix that indentation there. 1:28 That should be 4, thank you. 1:30 Okay, so models.model is the class that our models are going to extend. 1:33 Most of your models that you create throughout your use of Django are going to 1:40 come from this base class. 1:43 Sometimes you'll have a custom base class or 1:44 you'll be taking advantage of things like multiple table inheritance. 1:46 We're going to cover all that in a workshop course later on. 1:50 Now we need to add columns to our table. 1:53 And to do that we add attributes to our class. 1:56 We want to know when our class was first created, so we can see which instances 2:00 are old, and which ones might need to be replaced, all that kind of stuff. 2:05 Also what are the newest ones. 2:09 So let's add a field that we'll call created_at. 2:11 And it's going to be a models.DateTimeField and 2:17 so as you can probably guess, this is going to hold a date time. 2:24 We're going to set an attribute in here that is called auto_now_add and 2:29 we're gonna set that to True. 2:34 And so what this does is whenever a record is first added or 2:36 created then it will automatically set now. 2:40 The now, the current now is decided by the time zone that we set in our settings.py. 2:45 That's part of why we wanted to set that. 2:50 Okay, so what else does a course have? 2:53 Well a course probably has a title. 2:55 So let's create a title field. 2:57 This one'll be a models.char field and we have to provide a max length argument. 3:00 We have to say how many characters can this possibly hold, 3:06 at the uppermost level. 3:09 Generally I like to give people a little bit more room in here than I think they 3:11 will actually need so I'm going to give this 255. 3:14 It's probably overkill, but whatever. 3:18 I mean honestly if you can't name your course in 3:22 as many characters as fit in say a Tweet, then maybe you need to look at editing. 3:25 Okay, anyway, lastly, at least for 3:30 this initial version, our course probably needs a description so 3:33 the people know what the course is about beyond what the title says. 3:36 So, we're going to use yet another field type here. 3:40 So let's name this description, and this is going to be models.TextField. 3:43 And, so a text field is pretty free-form, as far as text content goes, and 3:48 it just holds text. 3:52 Whatever your text is. 3:53 It can be any amount of text, and most of the time you'll see these specified as 3:55 being empty, or a lot of times you will. 3:59 We're not gonna let this one be empty, so 4:01 they have to give us something, there has to be some sort of text. 4:02 So we have our model created, and we have our app in installed apps. 4:07 Let's look back at that again just to remind ourselves where that is. 4:13 So I have settings.py. 4:19 INSTALLED_APPS, there's our courses app. 4:21 Cool, there's our model. 4:24 So what we need to do now, if you remember when we talked 4:26 about migrations before it was all about moving them from one design to another. 4:30 Well our design has changed. 4:35 We've added a new table and that table has columns. 4:36 So we need to migrate our database again. 4:40 So we need to make migrations. 4:43 We do this with a manage.py command. 4:45 I'm going to get back into learning site and then I'm going to do python manage.py. 4:47 Let's just see what all's in here. 4:55 And there's a lot of stuff so let's move this so we can see what's up. 4:58 And there's just all these commands. 5:05 So if you go look in the docs, I'll link to it, there's tons and tons and 5:08 tons of commands here that are available. 5:11 They all do really awesome amazing stuff. 5:14 The command that we want though is, well it's kinda hard to get to, 5:16 right there, makemigrations. 5:21 So let's go ahead and use that. 5:24 We'll do python manage.py makemigrations. 5:25 And the migrations we're going to make are for the courses app. 5:28 And we can give a name like that if we want. 5:32 I'm not going to bother with that, I'm just going to run. 5:35 Just let it do this on its own. 5:39 So we'll press Return, and 5:41 we've got output telling us that it made the migration. 5:46 Let's just go look at it. 5:50 So if we come look at our migrations folder here, 5:51 we see this 0001_initial thing. 5:54 And this is what a migration looks like. 5:58 So we can see that it has an operations list here, and 6:01 it has this CreateModel object that it's creating. 6:04 The name is Course, the fields are an id, and I created that and a title and 6:08 a description, and these match what we said before. 6:13 The id one is given to us automatically by Django. 6:15 We don't have to specify that one ourselves. 6:18 This all looks good, all looks right. 6:22 So now we need to run the migration. 6:25 You probably remember how to do this, I am going to do python manage.py migrate. 6:28 And, we're going to specify the name of our app. 6:35 We don't have to. 6:38 If we don't specify the name, 6:40 Django just looks for anything inside installed apps that has un-run migrations. 6:42 But, we're going to tell it, hey. 6:48 Just migrate this one app. 6:48 So we're gonna do that, and it applies all the migrations. 6:51 Our model states are rendered correctly, and applied courses.0001_initial. 6:54 So, awesome, that means that our migration ran. 7:00 Everything's good to go. 7:03 Now that we have a model created and migrated, how do we use it? 7:04 In the next video, we are going to talk about that. 7:09 We will use Django's super handy shell command, I'll see you there. 7:10
You need to sign up for Treehouse in order to download course files.Sign up