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
Continuing with learning how to change the way your fields appear on the detail view in the Django Admin site, in this video you’ll learn how to replace a drop-down menu with radio buttons. You’ll also learn how to use TabularInline.
-
0:00
Now that we know how to group fields together into field sets,
-
0:04
let's learn a new way to change the way the fields look on the admin site.
-
0:08
Let's replace a drop down menu with a selection of radio buttons.
-
0:13
And then change some fields from stacking on top of each other to being in
-
0:17
a more tabular format.
-
0:19
First, let's replace the drop down menu for
-
0:22
Quizzes in the questions model with some radio buttons.
-
0:26
So what we're doing is replacing this drop down menu right here with a couple of
-
0:31
radio buttons instead.
-
0:33
And doing that is actually really simple.
-
0:36
So, all we do is scroll down here to our QuestionAdmin,
-
0:42
and we add this field called radio_fields.
-
0:46
And then we set it equal to a dictionary.
-
0:49
Now, the key is the name of the field in our model that we want to change,
-
0:54
in this case, quiz.
-
0:55
And then the value is going to be admin.HORIZONTAL in all caps.
-
1:02
This displays the options horizontally instead of vertically or
-
1:06
stacked on top of each other.
-
1:08
Since the user can only select one,
-
1:10
that means the options get displayed as radio buttons.
-
1:14
So now, whenever we go over here and
-
1:16
refresh, you can see those radio buttons instead.
-
1:21
This is especially handy if you have a field in your model that should only
-
1:25
have a few answer choices.
-
1:27
For us, those fields are which quiz a question should go with, and,
-
1:31
which course a quiz should go with.
-
1:34
The number of selections we have to choose from will grow over time, so
-
1:38
we might wanna stick with the drop down menu for now, but
-
1:42
this way you know about the option.
-
1:44
Now, let's go back to the list view.
-
1:47
Notice that the horizontal select option also affects
-
1:51
the list view if that field is editable in the list view.
-
1:56
For our last trick in this video, let's talk about tabular inline.
-
2:01
Tabular inline is an alternative to stacked inline.
-
2:05
You've already seen stacked inline.
-
2:07
It's how the text inline, quiz inline, and answer inline classes are set up.
-
2:13
So, for example, if we go into these true, false questions, you can see
-
2:18
that the Order, Text and then the Correct are all stacked on top of each other.
-
2:24
That's stacked inline.
-
2:26
Now, this is fine for a model like text, but for answers,
-
2:30
this one, it might be better to have things lined up across like a table.
-
2:35
Hence the name, tabular inline.
-
2:38
Can you guess where we're going with this?
-
2:41
That's right.
-
2:42
All we need to do, is make answer inline inherit from
-
2:47
admin.TabularInline instead of admin.StackedInline.
-
2:53
So we just change this to Tabular.
-
2:57
Refresh our page, awesome.
-
3:01
Now you can easily see all of the answers you've got for this question and
-
3:05
which one is correct.
-
3:07
It's much more readable.
-
3:09
Great work.
-
3:10
Now you know how to change the layout of the detail page,
-
3:13
including how to introduce a horizontal select as opposed to a drop down menu.
-
3:18
And how to change the presentation to stackedInline to tabularInline.
-
3:23
This will make changing information in your model much more intuitive.
-
3:28
Before you go on to the next video, can you think of any other models that might
-
3:31
benefit from some of the changes you've learned to make?
You need to sign up for Treehouse in order to download course files.
Sign up