Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, you’ll learn how to change the status of different courses to “in progress,” “in review,” or “published.”
We are almost to the end of our journey
through the Django admin together.
0:00
For our final task, we're going to
make some changes to our model so
0:04
we can add a status to our courses.
0:08
This way we'll know which classes are in
progress, which are being reviewed, and
0:11
which ones are live.
0:16
This will mean writing
some custom functions and
0:18
updating the is live field
on our model as well.
0:20
So let's get started.
0:24
First, we'll want to add the choices for
our new status field.
0:27
So we'll create this variable
called STATUS_CHOICES, and set
0:31
it equal to a tuple, and we'll just add
all of our status choices to this tuple.
0:37
Now we'll have three options.
0:42
In Progress, In Review, and Published.
0:44
We'll put each of these in its
own tuple with the one byte code,
0:48
plus a plain English description
of what the status means.
0:52
So we've got I for In Progress, R for
0:56
In Review, and
then finally P For Published.
1:00
Awesome.
1:06
Next we need to add the status
field to our course model.
1:08
So right down here below is_live,
we'll add this status field,
1:14
and it is a char field
with a max length of one,
1:21
because that is the length the one
byte code that we identified above.
1:27
The choices for
this field are going to come from that
1:30
status choices variable that
we identified right above, and
1:34
then we'll set a default value of i for
all of these.
1:38
Whenever you're adding
a new field to your models,
1:43
it's a really good idea to
add a default value, so
1:46
that you don't accidentally set everything
to published whenever you weren't ready to
1:49
do that, which is why we are choosing i
which, remember, stands for In Progress.
1:53
Now in our console,
1:59
we need to run a couple of commands to go
ahead and add these fields to our models.
2:00
So let's save our models.py file.
2:06
And then change directories
into learning site.
2:08
And first we'll run python
manage.py makemigrations.
2:11
And you can see that we've added
the status field to course, and
2:19
then python manage.py migrate.
2:22
All right, and we'll go ahead and
start our server just so
2:27
that we don't have to worry about
it here in a couple of minutes.
2:31
So, runserver Awesome.
2:33
All right, now, so that we can see our
status field, we wanna go ahead and
2:45
come over here to the course
admin class in admin.pi.
2:51
And we wanna add it to list display.
2:55
So we'll just add status,
and we'll come over here,
2:58
click on our little eye,
and pop into the admin.
3:04
And whenever we look at the courses, you
can see that we've got all of our courses
3:09
are currently in progress, which is great.
3:13
We know that the new field we added
to our model has taken affect.
3:16
Now we need to write our action.
3:20
So if you scroll up to
the top of this page,
3:23
you see this little Action menu here.
3:25
You see that the only thing in
it is Delete Selected Courses.
3:27
So you could select multiple courses.
3:31
And you could select delete and hit go and
3:34
it would delete those courses,
which is awesome.
3:37
So we're going to be adding
another bulk action so
3:40
that we can select multiple courses and
mark those as published.
3:43
So first, back over here in admin.py,
3:48
we need to scroll to the top,
and we need to define a function.
3:51
This function is gonna
be called make_published
3:56
cuz that's what we're doing.
3:59
So its arguments are modeladmin,
request, and queryset.
4:02
You can read more about these custom
actions in the Django documentation,
4:09
which I'll link to in the teacher's notes.
4:13
Next, we'll need to set the status of
everything in the query set, which is
4:16
everything that we've checked on our list
view, to have a status of P for published.
4:21
So we'll do a query set.update Status='p'.
4:26
We should probably also mark
the courses is_live field as true,
4:34
whenever we update it to published.
4:38
Finally, we need to add a message that
our users will see in the drop-down menu.
4:43
So for the delete courses action,
it says delete selected courses.
4:48
What we need to write here is the message
that the users will see whenever they want
4:52
to publish their courses.
4:57
So to do this, we do make_published, and
4:59
then short_description,
and then we just set
5:03
whatever we want our
short description to be.
5:08
So in this case it'll be,
"Mark selected courses as Published".
5:13
Finally, we need to add make publish
5:20
to the actions that are available
to the course admin.
5:23
So we'll go ahead and scroll all the way
back down here to course admin and
5:27
for actions we'll create a list, and we'll
just add make published to that list.
5:34
All right, let's go ahead and
refresh our page and see what we've got.
5:42
So now we've got this Mark
selected courses as Published, and
5:47
we can select maybe Ruby Basics and
Object-Oriented Python, and
5:50
hit go, and we can see that they're
both published, and they're both live.
5:55
Now this is a thing to be careful of,
because I actually made this mistake
6:01
whenever I was trying to film this course,
so if you select Python testing, and
6:05
you do mark selected courses as published,
but you hit save, nothing will happen.
6:09
You have to hit this go,
you have to select Python testing, and
6:16
you have to select mark selected
courses as published, and
6:20
hit this go, as opposed to this save,
if you want your action to take effect.
6:23
It'll be a little bit confusing, so
6:29
just keep that in mind if
you're having trouble.
6:30
Now, we probably, in order to keep
the status and the is live field in sync,
6:34
we probably don't want the is live to be
editable from the list view any more.
6:39
Instead, we might want the status to
be editable, so let's go ahead and
6:44
change that, too.
6:48
So for list_editable,
instead of is_live, we'll have status.
6:49
And we'll refresh to see
what this looks like.
6:55
Awesome.
6:59
And as you can see, having these green
checks and red x's makes it really
7:00
easy to see which courses are showing up
on the website, and now we can take for
7:04
example Python Collections, and
mark it as published if we want to.
7:09
Adding custom admin actions is
an easy to way to make bulk
7:14
edits to your model objects.
7:18
Why don't you add the custom actions for
Make In Review and
7:20
Make In Progress between now and
the next video.
7:23
It'll be great practice.
7:27
You need to sign up for Treehouse in order to download course files.
Sign up