Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Changing Field Order3:13 with Lacey Williams Henschel
Change the order of the fields on the update screen.
Sometimes we add fields to our models in the order we think of them, and 0:00 not based on any sort of system or design, and that's fine. 0:05 Django does not care what order the fields are in. 0:09 Neither does the admin. 0:12 But the admin is always going to put the fields it's displaying 0:14 in the order they appear in the model, 0:18 which might not be an order that makes sense when you're updating. 0:20 Listing all of the ingredients in the ice cream 0:24 before you give the name of the flavor doesn't make a lot of sense, right? 0:27 So open up Work Spaces and let's put some fields in a more logical order. 0:31 Now, let's change the order of the fields in one of our update screens. 0:37 Click on add next to quizzes. 0:42 This detail page has a lot of fields in it. 0:45 It might be more helpful to have some of these fields in a different order. 0:48 For example, 0:51 the most important piece of a quiz is probably which course it goes with, right? 0:53 So, we might want to move the course selection to the top, 0:59 instead of having it all the way down here at the bottom. 1:02 Let's go back to work spaces, and 1:05 open up our admin.pi And 1:08 let's add a new class called quiz admin and have it inherit from model admin. 1:13 So right here below QuestionAdmin, just just click below here and 1:19 add a new class, and it'll inherit from admin.modeladmin, 1:25 and let's just include one more space here. 1:31 We don't wanna violate pep8. 1:35 Now we wanna make a variable called fields. 1:40 And assign to that a variable a list of all the fields that we want 1:45 in the order that we want using the names of the fields from our model. 1:49 So in this case, let's have the course come first, 1:54 followed by the title of the quiz, a description of the quiz, 2:00 the order, and then the total questions. 2:07 And close the list. 2:14 Now down here at the bottom, where we register our models, 2:17 where we have this admin site.register(models.Quiz), 2:21 we have to path in the QuizAdmin as well, just like we did with course. 2:25 So we save all of this, and let's refresh our page. 2:30 We got a nice error. 2:36 Unknown field. 2:37 A total question is a thing that needs an S on it. 2:38 That's what we got. 2:43 Let's see. 2:44 And there we have it. 2:47 Now we have the course at the top, followed by the title, the description, 2:49 the order, and the total number of questions. 2:52 Wasn't that awesome? 2:56 Customizing the Django admin is really straightforward 2:58 once you know what you're doing. 3:01 Why don't you take some time to practice by reordering some of the other fields on 3:03 the other models in the admin. 3:08 I'll see you back here in the next video. 3:10
You need to sign up for Treehouse in order to download course files.Sign up