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
Edit the items in your database directly from the list view, without going to the detail view! This makes it easier to do bulk edits, like if you need to change the titles of several classes at once.
Resources:
Django Admin documentation on ModelAdmin.list_filter
Django Model documentation
Python documentation on the math module
We are almost done with the list view.
0:00
At the end of this video, you'll be
able to do something incredibly useful.
0:03
Make edits to your objects in your
database straight from the list view.
0:08
You won't have to click
through to the detail view for
0:12
every little change you wanna make.
0:15
Instead, we'll learn how to make bulk
edits straight from the list view.
0:17
So let's get started.
0:21
You're probably thinking that making
fields editable in the list view is going
0:24
to be super complicated.
0:29
But hopefully you've learned that
customizing most things in the Django
0:30
admin is really straightforward and
this is no exception.
0:34
To grant yourself the ability to change
your objects directly from the list view,
0:39
you just need to add one simple attribute
to your admin class, list editable.
0:43
Now remember,
that you can't make something editable
0:49
in the list view that isn't
already in the list display, so
0:52
you have to have a list display first
before you can add list editable.
0:56
So let's practice this
with the QuestionAdmin.
1:01
So for
QuestionAdmin we wanna add list_display.
1:05
And we wanna display the prompt.
1:12
And then maybe also the quiz that
that particular question goes with.
1:14
And then the order that question comes in.
1:20
Now to make those fields editable,
1:22
we just make a variable called
list_editable and assign to it the list
1:25
of the fields that we want to be
editable from the list_display.
1:31
Now, let's try this out with quiz and
order.
1:36
It makes sense to have the quiz and order
fields be editable from the list_display,
1:42
because they're pretty easy
to understand and correct.
1:47
If someone makes a mistake when creating a
question and assigns it to the wrong quiz,
1:50
that's the kind of thing that's easy
to see and correct at a glance.
1:55
Which is exactly what being able to
edit from the list display is for.
1:59
So, if your server hasn't already been
started, go ahead and start that and
2:03
let's take a look at this in action.
2:06
All right, so just to get here, just to
review, I went to any of the questions.
2:11
So you could go to True false questions or
Multiple choice questions.
2:15
And now I can see which
quiz that this question,
2:19
Which of the following is an invalid
string container, goes with.
2:22
And I can see the order that it goes in.
2:26
And both of those things are changeable.
2:28
So I can change this to New Quiz, and back
to Review and back again if I want to.
2:30
Try it for yourself.
2:37
Now, booleans, or true false questions,
2:39
in the list display
are a little bit different.
2:42
You've seen that they show up as a green
check or a red x in the course display, so
2:45
let's see how they show up
whenever they're editable.
2:50
So we can go back up to CourseAdmin.
2:52
And we can add, list_editable.
2:55
And let's see what our is_live
field looks like whenever we make
2:59
it editable from the list view.
3:04
So we'll go over to Courses.
3:08
So as you can see,
instead of the green and red graphics,
3:12
you have these checked and
unchecked boxes.
3:15
Try selecting and
deselecting some to see what happens.
3:19
Now, be careful with this.
3:25
If you enable editing in the list view, it
means you could be setting up a condition
3:27
where you're editing some objects in
the list view when someone else is
3:32
editing those same objects in the detail
view or even in another list view.
3:36
This is called a race condition.
3:41
Whoever made their edit last, wins.
3:43
That was incredible!
3:46
As you can see, there is so
much you can do to customize the list view
3:48
of the Django admin and
make it work better for your projects.
3:52
You can change which fields appear
in the list view, add searches and
3:57
filters, and
even edit straight from the list view.
4:00
It's important to think about which fields
would be best to change from the list
4:06
view, versus, which fields
should stay in the detail view.
4:09
For example, changing live to not live,
or the quiz that a question goes with,
4:13
is the kind of thing you can see and
correct at a glance.
4:19
But a course description
can get pretty long and
4:22
might take up too much
space in a list view.
4:25
In the next stage of this course,
you'll learn how to make
4:29
similar customizations to the other part
of the Django admin, the detail view.
4:32
This is the screen you see when you click
on a specific object in your database,
4:38
like a course or a quiz, and
then make changes to it.
4:42
I'll see you in a bit.
4:46
Right after you crush this code challenge.
4:47
You need to sign up for Treehouse in order to download course files.
Sign up