Exclusivity6:53 with Kenneth Love
The `.filter()` method let's us filter things down by more than just relations. Let's explore these other lookups and how to use the `.exclude()` method.
The conditions are called field lookups and there are a lot of them. The ones I seem to use the most often are
gte (greater than or equal to),
lte (less than or equal to),
in (uh, just like Python's
in keyword), and
in for strings). I'm sure you'll find some that you love, too.
The condition can also be a related model, so, for example, if
C has a foreign key to
B has a foreign key to
A, you could do:
C.objects.filter(b__a__id__in=[1, 5, 10]) to get all of the
C objects that have a
B object with an
A object whose
id attribute is 1, 5, or 10. Yes, this is an over-engineered, overly-complicated scenario, but you'll be surprised how often similar things come up.
If you're coding along and the form isn't submitting when you click the button, change the button's
type attribute to be
"submit" (or, better yet, turn it into
<input type="submit"> with all of the other classes applied).
We can use the filter method to filter records based on the value of their own 0:00 attributes, or the value of a related model's attributes. 0:04 But what if we need to filter based on a condition of the attribute, like 0:07 that it contains the characters D-O-G, or that it's a number greater than five? 0:11 We can do that with field lookups. 0:16 Let's go check them out. 0:17 I think we should make a search form and a view, like right over here. 0:20 That we can use to look for courses that are specific words or 0:25 characters in their title. 0:28 So first we need to add the form to our layout template. 0:30 Now this might seem a little weird to some of you that I'm gonna do it this way. 0:34 It's not in the new site it's in templates. 0:40 Layout. 0:44 But I promise you there's a method to this madness. 0:46 So let me add this in real quick, and then I'll talk about what we're gonna do. 0:49 So section class equals top bar right. 0:53 And then, form action. 1:00 We don't have an action just yet. 1:02 Method is, GET, by default, but it's nice to be explicit. 1:05 And type equals search. 1:16 Name equals Q and then. 1:18 And we'll say search on the button because our life form is actually. 1:33 Okay, so now, why am I doing it here? 1:39 Why am I writing the HTML and the reason is actually pretty simple? 1:42 The reason I am writing the HTML is because if I don't write 1:45 the HTML then I have to go and instantiate this form on 1:50 every view where I have to make it a template tag or something like that. 1:55 And while I can do that and it's totally cool and 1:58 fine to do that, it's not really a thing I wanna do. 2:00 I'd rather just hard code thing and be done with it. 2:06 So we're gonna do search, And 2:09 I'm just gonna go to views.search and our name will be search. 2:12 Pretty simple. 2:18 So let's go fix this with our URL will be courses:search. 2:20 All right cool. 2:28 So we have the URL, we have the template. 2:29 We need to go and create the view. 2:31 All right. 2:35 So this is gonna be very similar to our courses by teacher. 2:36 So we will do Def search, 2:40 and it's gonna take a request. 2:45 And term is gonna be equal to request.GET.get Q. 2:48 So hopefully it's there. 2:53 And then we will do courses=models.Course.objects.filter. 2:56 Title. 3:03 I don't want to do title equals though I want to title__icontains. 3:04 And then the term. 3:09 And so 3:11 up here we use the double underscore to go from one model to the next. 3:12 Right, like we're on the course model gonna look at their teacher. 3:18 The teacher is a foreign key. 3:21 Okay on that foreign key related model, there should be a field called user name. 3:22 What we're doing here is kind of similar. 3:26 We have the course model. 3:29 Go look at the title. 3:30 Title is in a foreign key, title's an actual thing. 3:31 The title should contain whatever this term is and 3:34 the i says make this case insensitive. 3:38 So if I search for the word apple, all in uppercase, and 3:41 the title has the word apple in lowercase, but with a capital a, it'll still match. 3:44 So cool. 3:50 And then we want to return render request, 3:52 courses/course list.html and 3:55 courses as courses. 4:02 All right, cool. 4:06 So let's go over here and let's refresh our page. 4:08 Nothing here by geoduck still. 4:12 And then let's search for something, so 4:14 I'm gonna search for testing, actually search for test. 4:15 Look, cool, I got one thing. 4:19 Python testing. 4:20 So that's there, let's try like OBJ. 4:21 All right I'll return a Python Java objects. 4:26 All right cool. 4:28 So let's try searching for something that shouldn't exist. 4:29 Let's try earthworm. 4:31 I don't think earthworm is in any of our courses and it's not cool. 4:32 So we could expand this a little bit further we could look for 4:37 letter sequences in our descriptions. 4:41 Or we could like make sure they I mean is I can submit A, and 4:43 that's gonna find everything everything as an A in it somewhere. 4:47 We could do it so that doesn't do that, we could do those things. 4:49 Those are going to require a little more knowledge than we already have, 4:52 at least the descriptions one will. 4:54 So we'll come back to that in a little bit. 4:56 You go ahead and do the things that are too short. 4:58 If you want. 5:00 Now what if you wanna skip records that match your query? 5:01 Instead of using filter we can use exclude and 5:04 find records that we don't want to include. 5:07 I think is a little easier to try in the shell. 5:10 So let's pop this up and I'm going to Python manage.py shell. 5:13 I'll add up a little higher with that even, all right. 5:21 And so then I had to do from courses.models 5:24 import course, and here's what I want. 5:28 I wanna do course.objects.exclude where the subject 5:32 is in Python or Java. 5:37 So I want all the courses that are not about Python or 5:41 about Java and I get a lot of stuff. 5:44 Now why did I get these one, two, three, four things? 5:48 Because those existed 5:52 before I loaded the fiction data they don't have a subject set. 5:53 But nothing else in here has that. 5:57 In fact, let's go python = 5:59 Course.objects.filter(title_icontains 'Python'). 6:01 All right? 6:11 For course in python: course.subject="Python, 6:12 course.save(). 6:19 All right, cool. 6:22 So, now if we were to rerun this thing. 6:24 And we don't get ours so cool. 6:29 Now everything works and my data is correct. 6:30 Yeah. 6:32 There are a lot more field lookups available and 6:34 we'll be using some of them during the rest of this course. 6:36 I've also put a link into the teacher's notes to the documentation about them so 6:39 be sure to check it out and play around with them by yourself. 6:42 All right. Take a little break get a drink. 6:45 Take a walk around the block and then come back for my next update video. 6:47 The next video is about updating records. 6:51
You need to sign up for Treehouse in order to download course files.Sign up