Step by Step7:25 with Kenneth Love
Our Courses wouldn't be complete without Steps for students to follow.
IntegerField is a field that holds integers, or whole numbers.
An inline is a smaller form inside of a larger form. The smaller form represents a related record in the database.
StackedInline is an inline where each field takes up the full width of the form. Fields are stacked.
TabularInline is an inline where each field is part of a single row for the form.
More docs on inlines.
We have courses, but each curse should have steps to it. 0:00 Maybe it will be videos, or maybe it'll be text based steps, or 0:03 images, whatever method you want to teach with. 0:06 Since I don't know what your step of choice will be and 0:08 we're not gonna deal with video or image uploads. 0:11 We'll just call them steps. 0:13 Anyway, let's get to it. 0:15 So since steps are a logical part of our courses, we'll keep them in the same app. 0:16 So let's close some of this stuff up again. 0:22 It gets a little unwieldy at times. 0:25 So inside courses, inside models.py, we're gonna add our new model. 0:29 And we'll say class Step(models.Model). 0:39 So what goes into a step? 0:44 Well I think a title deserves to be there. 0:45 And this will again be a char field of, 0:49 255 characters or less. 0:54 I think we would have a description just like our course would be. 0:58 So so far a step and a course are the same thing just the created app. 1:04 I think we would have an order field, 1:09 so that we could specify what order different fields come in. 1:11 And we'll set the default on this as zero, and then let's do a course, 1:17 and this will be a foreign key, and this will link back to course. 1:23 And since we know we're going to print these out eventually, 1:32 let's do return self.typo. 1:35 Okay, so there's a lot here, the first two items should be pretty familiar from our 1:37 course model, they're exactly the same. 1:42 The last two items though are new, and I briefly went over them but 1:44 let's talk about them a bit more. 1:48 Order lets us control the order of steps. 1:50 Maybe we forget to create a step and we have to create it later. 1:53 This way, we can add a new one, and we can set where it should be in the order. 1:56 Setting it to zero makes it so 2:00 we can set other numbers to control the ordering of steps later. 2:01 There are other solutions to this. 2:04 you could have them auto-increment. 2:06 You could set them at a negative, like, there's different ways to do this. 2:07 Zero is a pretty safe default. 2:12 That we ordered automatically by their ID, and 2:14 then we'll handle the other ordering later. 2:18 And then, the course attribute is a foreign key. 2:20 And a foreign key is a record or a column that points to a record in another table. 2:23 Can be on the same table, but for now it's in another table. 2:30 So, what we're doing is we're saying, hey here's this step. 2:32 And maybe this is a step ID of three and it belongs to the course with 2:36 an ID of one, so it belongs to that course over there and points to that course. 2:42 This way, a single course can have multiple steps, but 2:48 each step only has one course. 2:53 This is what is known as a one to many relationship, or 2:56 we're looking at it from the step side, a many to one. 3:00 Many steps belong to one course. 3:03 And then of course we have our Dundar STR method, 3:06 which will print out the title of the step. 3:09 You might want to print out the order but 3:10 I'll let you put that in there if you want. 3:12 So now that we've made our our model. 3:14 Got ahead of myself. 3:17 Now that we've made our model, we, of course, have to create a new migration. 3:18 You'll do this dance a lot. 3:22 You'll make a model, then you'll make a migration, 3:25 and then you'll apply the migration. 3:28 It might seem monotonous and 3:30 time consuming, but it's good to know exactly what's going on behind the scenes. 3:32 So, let's do this. 3:35 Python manage.py, make migrations for courses. 3:36 Hey, we had to make one, cool. 3:42 Python manage.py, 3:44 migrate courses. 3:49 Done. Applies all my migrations. 3:53 Okay. 3:54 So we've made our model. 3:55 We can play with it in the shell if we want to. 3:56 But, I think it's easier just to stick it in the admin and 3:59 then go mess with it in the admin. 4:01 And I bet you agree with me. 4:03 So let's run the server again. 4:05 And then shrink our terminal down. 4:08 And let's go look at admin.py. 4:12 And let's import Step and let's copy and paste this line and change Course to Step. 4:15 So we've imported it, we've registered it, things are cool right? 4:22 Let's go back to our admin and check it out. 4:26 /admin, there's our steps, there's our courses. 4:27 Okay, so let's go add a step. 4:35 So, we'll add a step, give it a title, and we'll say What's the deal with strings? 4:37 Strings are more than just a bunch of letters. 4:46 Find out why! 4:52 Okay, and the courses belongs to, this is course of Pythons basics step. 4:56 So let's go ahead and save that. 5:01 Cool, now this isn't terrible but 5:03 we if we had a lot of courses this might get really difficult. 5:05 That drop down, that can take a while to read through and find stuff. 5:08 Probably a better way, and you know I wouldn't ask that unless it was true. 5:13 Django lets us create smaller forms that represent another model 5:17 inside the admin form for a model. 5:21 So we can edit and create steps for our courses while we're editing and 5:24 creating a particular course. 5:27 We need to create two new classes for this to work though. 5:29 So let's hop back over to our editor. 5:32 I get to do all of them right here inside admin. 5:35 So now first we need to make the class for 5:38 our smaller form and that's known as an inline. 5:40 So we're gonna say class StepInline(admin.StackedInline). 5:43 Now there are two types of inlines. 5:50 There are stacked inlines and tabular inlines. 5:52 We're gonna use the stack version, but feel free to switch over to tabular and 5:55 try that out. 5:59 And we said our model is equal to step, now that's it. 6:01 On it's own, this doesn't do anything, but this creates the inline. 6:06 So now We have to make our next thing, which is our course admin. 6:11 This is the admin for handling our courses. 6:16 This let's us specify custom special things. 6:20 So this is (admin.ModelAdmin) and we're gonna set inlines = [StepInline,]. 6:23 All right, now this is the one thing that's a little bit weird. 6:32 When we made this StepInline, we set model = Step. 6:35 We didn't set a model on course admin, we do there here. 6:39 So we register course, and then we register it with course admin, okay, and 6:42 if we want we can delete this, but let's leave it alone for right now. 6:47 So let's come over here and look at this again, let's go back to courses. 6:50 Courses, and let's look at Python basics because we know it has a step. 6:54 Python basics, and look, here's our step. 6:59 What's the deal with strings? 7:01 Strings are more than just blah blah blah, there's the order. 7:02 Here's where we can add a second one, and a third one, and a fourth one, and 7:04 if we need to add more we can click add another step. 7:09 So that's pretty awesome, we can edit, delete, and 7:11 we can add new steps right here in the admin. 7:13 There's a lot more that we can do with inlines but this is enough for now. 7:17 I've linked to more documentation on them in the teachers notes though, so 7:20 feel free to explore them some more on your own 7:23
You need to sign up for Treehouse in order to download course files.Sign up