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