Take Control8:26 with Kenneth Love
Sometimes we don't want the actual objects, or we don't want objects that are repeated. Let's see how to take control of our results.
.order_by() can accept any field or multiple fields, and can ever traverse relationships. Super handy. If you order by multiple fields, the first takes precedence and the latter fields are used only in places where the previous ones conflict.
Sometimes we don't want or need the entire model instance from the database. 0:00 Like maybe we're making a list of people coming to our party. 0:04 We could have just their names and be okay. 0:07 Or sometimes we get a bunch of records from the database, 0:10 when really we just need the dates that those records were created. 0:12 And even more other times, 0:15 we need to make sure that all of our records are sorted in a certain way. 0:17 Django's ORM gives us tools for all of these scenarios. 0:20 So let's start by taking another look at this nav_courses_list. 0:24 The custom template tag that we built with Lacey. 0:29 This template tag gets five courses and sends them to the template. 0:32 In particular, it sends them to this template. 0:36 I've updated this one a little bit so 0:39 that it only gets the published ones, but still, we can only get five. 0:40 I wanna change this so 0:44 that it uses values instead of just sending out the whole thing. 0:45 So we're gonna say values. 0:51 Okay, cool. 0:54 We need to update this template a little bit too though. 0:55 So let's go look at that, and this template is the course_nav. 0:57 All right, so inside this template here, 1:02 we're currently using this dot notation, and we're not gonna be able to do that 1:04 because values gives us a dictionary of model attributes. 1:09 So there are keys and not attributes. 1:13 Also, pk isn't an attribute, it's this alias that Django sets up for 1:15 us of the ID attribute. 1:20 So we need to change this to ID. 1:22 Okay, that's it, that's cool. 1:24 Let me run my server. 1:27 Okay and if I refresh, then cool, everything is still there. 1:36 So that's great. 1:41 So values gives us a list of dictionaries, one for 1:42 each model instance that was selected. 1:46 The dictionary's keys are the models attributes and their values are, 1:47 well, the values. 1:50 This isn't as useful when we're using it the way we are right now, 1:52 because we're getting the whole model instance anyway, 1:55 and then we're turning it into a dictionary. 1:57 We look here, we give them the whole thing, and 1:59 then we're turning it into a dictionary. 2:02 Cool, but not really all that amazing. 2:04 We can change this though so that it only gets out the fields that we need. 2:07 If we look at our template, we're only using two fields. 2:11 We're using id and we're using title. 2:14 So that's all we need, we don't need the whole thing. 2:17 So, let's just get those two things. 2:21 So here inside the values call, we do 'id', 'title'. 2:23 All right, so that's cool. 2:29 Let's actually look over here, let's look at our SQL queries. 2:31 Wait, I have to reload it. 2:36 Anyway, 41 milliseconds to do four queries. 2:37 So we dropped off a millisecond, maybe. 2:42 But I wanna show you what this one looks like. 2:45 If we look here where it has the limit five, 2:47 what's cool is you can see that we're only selecting the ID and the title fields. 2:51 If we look down here where we're looking at all of our courses, 2:55 you can see that we're selecting like each and every field. 2:58 So it's kinda cool to see that we can limit this and it's a little smaller. 3:01 Okay, so that's all great. 3:05 That's wonderful. 3:07 So yeah, there's not a lot of time. 3:09 On some stuff it will be way more impactful. 3:11 If you have a really large model or really large number of items that are coming out, 3:13 then this will impact a lot more. 3:17 And often it's better to just control exactly what comes out. 3:20 Now similar to values is values_list. 3:23 This will give us a list of tuples instead of a list of dictionaries, and 3:28 each field request is in the tuple in order. 3:32 And if we leave it blank, 3:34 then we get all the attributes of the model in their defined order. 3:35 This isn't as useful in this situation, so 3:38 I'm not even gonna bother showing it to you. 3:41 Because we would have to go change this, change template, and 3:44 other stuff like that. 3:46 Values_list is really useful though, when you need to produce an ordered structured 3:47 representation of your model instances. 3:52 You might find yourself doing something like this when you're 3:54 providing information to a third party, for example. 3:56 Let's pop down to our terminal for the next method. 4:00 Come down here, control C, 4:03 I'm actually gonna do manage.py shell, 4:07 and from courses.models import Course. 4:12 I want to see all the dates that my courses were created at. 4:17 So let's do dates = Course.objects.datetimes('created_at', 4:21 and what I wanna get out of these is I wanna get their year. 4:28 All right, so if I look at dates then I get 2015 and I get 2016, 4:33 I've created stuff in two different years. 4:37 So that's neat. 4:40 Now, our models have datetime fields for the created_at, so 4:42 we have to use the .datetimes method. 4:46 If we had date fields instead, where it just holds onto a year, month and 4:49 day, then we could use .date instead of .datetime. 4:54 So what's cool though, this is the list of datetimes. 4:58 And they're representing the year. 5:00 Notice the dates are the same and the time zones are the same and 5:01 the times are the same. 5:05 But the years are different, so that's kinda cool. 5:07 Let's do this again, let's do this as months. 5:11 And we get something a little bit different. 5:16 So you can see, we have 2015, 6, 1, 2016, 1, 1 and 2016, 2, 1. 5:18 So, we have three different months, because the years are different and 5:24 the months are different, where we've created these objects. 5:27 So, that's kind of neat. 5:30 Ours are a little bit artificially compact because we're doing this as courses and 5:31 things like that. 5:36 If this was on a real site with real user entered content, 5:36 we'd get a lot more years, months, days, all that stuff. 5:39 And you could use this to make an archive that's browsered by year, 5:42 month, day, whatever. 5:46 Datetimes is cool, I don't think we have a place for it in our app, but 5:47 I just wanted to show it to you. 5:50 Let's go back to our template tag, get my server running. 5:52 All right, so our template tag, I have a small problem with it. 5:56 Right now, it's getting five item, but it's always gonna get the same five items. 6:03 It's always going to be, let's hide view. 6:07 So it's gonna be these same five items, because it gets the first five and 6:10 those are the first five that were entered. 6:13 I want it to get the newest five. 6:15 Now we can't use the latest method like we did here, because latest only gets 6:18 a single item, cuz there is only one thing that's the latest. 6:22 So we need to change the ordering of the courses so that we get the newest one. 6:26 So this is easy enough. 6:33 We've done our filter. 6:34 And then let's do order_by, and the way we want to order this is by ('-created_at'). 6:35 I'm going to do this here real quick. 6:46 Pop down to published. 6:48 Just cuz that's getting so long. 6:51 There we go. 7:01 Maybe not more easily read but I think it is. 7:02 Okay, so what does this mean? 7:05 The '-created_at' is the field that we want to order it by. 7:07 Okay, so I want these ordered by their created_at field. 7:12 And normally we go in ascending order. 7:15 If you're not familiar with the idea of ascending or descending, 7:18 think about an ordered list, right? 7:21 If we have the numbers one through five, 7:23 if I go from one to two to three to four to five, I've gone in an ascending order. 7:24 It's gotten bigger. 7:29 if we're doing descending order, I would go 5, 4, 3, 2, and 7:30 1, because they're getting smaller, they're descending. 7:33 By putting in this minus sign, I've said to do this in descending order. 7:37 So the dates will get older as we move through. 7:40 So, save that, go refresh this, and I should have brand new courses. 7:43 Yeah, these are our newest courses. 7:50 So that's kind of cool, we can kind of show off our new stuff. 7:52 At this point, you've learned more about the ORM than you ever wanted to know. 7:56 Each of these methods from bolt_create to order_by to exclude, 7:59 all have their own use cases. 8:02 Each of them has a particular area where they're the absolute best solution. 8:04 Knowing all of their ins and outs and details isn't necessary. 8:08 But knowing that they exist and a general idea of what they do will 8:11 save you a bunch of time as you continue on in your Django career. 8:14 Take a little break so all of this can sink in. 8:18 Then come back to learn about a couple of amazingly powerful tools the ORM gives us 8:20 for building fancier queries. 8:24
You need to sign up for Treehouse in order to download course files.Sign up