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
Add a custom attribute to our model that we’ll then display in the list view of the admin site.
Resources:
Django Admin documentation on ModelAdmin.list_filter
Django Model documentation
Python documentation on the math module
If you've taken customizing Django
templates, you might remember writing
0:00
a custom filter that estimated how long it
would take someone to complete a course,
0:04
based on how many words were
in the course description.
0:09
It's not the most scientific method
of estimating time to completion, but
0:12
that's fine.
0:16
Now we're going to use the same
function we wrote in that course
0:18
to add a custom attribute to our model
that will then display in the admin.
0:21
Remember that custom template tags
are just regular Python functions, so
0:27
we can import that function
into another module and use it.
0:31
So, let's get started.
0:35
Open up course_extras.py.
0:37
So you see this function
here time_estimate?
0:42
This is the function
we're going to be using.
0:45
Now if you scroll to the top,
you'll see that we're all ready importing
0:48
the course.models into course_extras,
which is great.
0:52
We don't wanna change that.
0:56
But it makes things a little tricky
when we import this function
0:58
into our models.py file.
1:02
Since we're going to be importing
time_estimate from coarse_extras.py into
1:03
models.py., but since course_extras.py is
all ready importing one of the models,
1:08
we would get an error if we added the
import statement to the top of models.py.
1:14
Basically, we'd be trying to
do a recursive import and
1:19
Python would be, what?
1:23
So instead, let's go into the course
model and add our method,
1:25
which we will call time_to_complete and
1:29
then we'll add the import statement
directly inside this method.
1:32
This will get us around any sort
of recursive import errors that we
1:35
might encounter.
1:39
So we define time_to_complete and
then we add the import statement.
1:41
So, from
courses.templatetags.course_extras
1:47
import time_estimate.
1:55
We'll also want to return the result
of calling the time_estimate function,
2:00
so we can go ahead and add that as well.
2:05
Awesome.
2:07
Now, remember that time estimate
is based on the number of words.
2:09
So, we need to pass in
the number of words or
2:13
the word count of the course description
into the time_estimate function call.
2:16
Whenever we did this in
customizing Django templates,
2:21
we got the word count using one
of Django's built-in filters.
2:24
But in models.py,
we don't have access to that.
2:28
Instead, we can get out the word
count using a combination
2:31
of the built in Python functions len and
split.
2:35
So, we'll just get the length
of self.description but
2:39
that's just going to give us
the character count or the byte count.
2:45
So instead, we add split and
then that will get us the word count.
2:48
So save that, and
now all we need to do is add the time to
2:54
complete attribute to the list
display in our CourseAdmin class.
2:59
So, we go into admin.py and
right here in the CourseAdmin,
3:03
inside the list display variable,
we just add time_to_complete.
3:08
All right, save that and
then start your server,
3:14
if it's not all ready started,
and refresh your courses list.
3:18
And now you see that we have these
numbers underneath time to complete, and
3:24
remember, these numbers stand in for
minutes.
3:27
Now, one quick caveat.
3:30
When I refreshed mine the first time,
3:32
all I got was 0 minutes across the board
because our descriptions are so short.
3:34
So, I went into a couple of courses, and
I replaced the course description with
3:39
some sort of fun dessert-themed lorem
ipsum text just to bulk up the word
3:43
count in the description and illustrate
how the time to complete would work.
3:48
So feel free to Google,
like fun lorem ipsum and
3:53
experiment with this for yourself.
3:56
But as you can see, a number all by itself
like this this random 14 or this 25,
3:59
is not super helpful.
4:04
It's useful to have the unit of
measurement, like the minutes.
4:06
And so we can add this to the method
on our model to return the minutes.
4:10
So, we can return this
whole thing as a string.
4:14
So, if we go back into models.py and we
add some things to this return statement.
4:17
So we open up a string and
then we add these brackets and
4:23
then min and close the string and
then we add format and
4:28
we enclose the whole rest of this
line inside the format call.
4:32
And we refresh our page.
4:39
Then we have the minutes added so
we can see that Ruby Basics takes 14
4:42
minutes versus Python testing,
which apparently takes no time at all.
4:46
That looks a lot better.
4:50
Great work.
4:52
As you can see,
adding custom attributes to your list view
4:54
is as easy as writing a little method to
bring back the information you need and
4:57
then adding it to your admin class.
5:02
Now, we've got this handy piece
of information at our fingertips.
5:04
We could even use this to add
an extra filter to this page.
5:08
Why don't you try that on your own?
5:12
You need to sign up for Treehouse in order to download course files.
Sign up