Mind your Ps and Qs5:32 with Kenneth Love
Django gives us two really handy, but hard to grasp, tools for doing logical ANDs and ORs in our queries and using the values already stored in our database fields in other queries.
We can't always put all of our query logic into a filter or exclude method. 0:00 Sometimes we need to build queries that are more logically complex. 0:04 For example, we might need to use ors, where records can match one or 0:08 more conditions, ands, where records have to match two or more conditions or 0:11 negated ors where records negatively match one of more conditions. 0:16 That's a lot to say and even more to take in. 0:20 Let's go to work spaces. 0:23 So our search view is pretty cool. 0:24 I mean, we can search titles right? 0:27 I wanna search more than just titles though. 0:30 Sadly we can't do this with just another filter. 0:32 I kind of wish we could. 0:35 Let's let's bring our filters to somewhere just a little bit easier to read them. 0:36 Okay, so if we would add another filter in here. 0:41 So, description__icontains=term. 0:45 This is cool. 0:50 This is a great, but this actually creates an and condition. 0:52 So the term that they've sent in, we'll say the word Apple or 0:55 Python, has to be in both the title and in the description. 0:59 Now that'll work for some searches, but it won't work for all of them. 1:03 So maybe we add it as another filter, right? 1:06 So let's take that out. 1:10 And then we do, .filter, that thing. 1:13 Yeah? 1:16 So, kind of except that what this does is we'll actually do this filter first. 1:17 So we'll get things where the title contains that term and 1:22 the published is true. 1:25 And then, out of those records, 1:26 we'll filter them to where the description contains that term. 1:29 So, we still have the exact same problem where it's 1:32 both these things have to be true, but we've just kind of moved it around. 1:35 Kind of mitigated it again. 1:39 It's not what we want. 1:40 This is where we get Q objects this is where Q objects come in really, 1:43 really handy. 1:47 So the first thing we have to do is we have to do an import. 1:48 So up here from django.db.models. 1:51 That's it. 1:58 Import q. 1:59 So that actually belongs right there, all right. 2:00 And then back down here in search we're gonna make our q objects. 2:05 So I'm gonna do this a little bit different here. 2:09 I'm gonna do published=true. 2:11 And then I'm gonna do a filter here. 2:17 You'll see why in a minute, it's just to make this a little easier to read. 2:20 All right, so cool, so we have our filter published=True, and 2:25 then now we're going to do our Q objects. 2:29 So we're going to do a Q Title icontains=term and we're gonna a pipe and 2:31 I'm gonna do a Q description__icontains=term. 2:38 So, what does that do? 2:42 Well the Q objects are actually entire queries all on their own. 2:45 They're like sub queries. 2:49 The Q actually stands for query. 2:50 So the first query here says, find records with a title contains case insensitive, 2:52 the term. 2:57 The second query says the exact same thing, but instead of title it does it for 2:58 the description field. 3:01 The pipe character here in between these, that makes this an or. 3:03 It effectively does like a union on these. 3:08 So anything that's in the left side comes up and 3:10 anything that's in the right side comes up. 3:15 Since either side can be true, we get the results back from both sides. 3:18 So that's pretty cool, that's really handy. 3:22 So let's try it out. 3:25 Over here to our search and let's try tuple cuz I can see right there, 3:28 it's under description. 3:32 So, cool, I got that one. 3:36 Let's go to all courses and let's see about tool. 3:38 Okay that comes up in there. 3:46 It's in that description. 3:47 What if I search for Android? 3:48 So symbol title but it's also in the descriptions, so I get both of those. 3:51 That's pretty cool. 3:54 I think I actually try one more, I wanna try app. 3:55 So this is a neat one. 3:59 Here app is in the description, and app is in this description. 4:00 But here app is in the title, and yeah. 4:06 That looks like that does pretty much what we want. 4:11 So that's neat. 4:13 If you provide more than one queue object and you don't use the pipe or 4:14 you wanna use keyword lookups alongside your queue objects, 4:18 they get anded together where they both have to be true. 4:21 This is similar to putting more than one thing into a filter, 4:25 we can actually use these all at once. 4:27 Let's try this to reduce the number of calls that we're doing here. 4:29 So we'll do a comma, and will do published=true. 4:33 All right, so let's take out those two lines, all right. 4:39 Doing as few lines as possible is always a good idea. 4:44 And if we go refresh our page, still works. 4:47 Now we can't swap these two around. 4:52 We can't do published equals true before the Q objects. 4:55 And the reason we can't do that is because the published equals true is technically 4:58 a keyword argument, it's a quarg. 5:02 And the Q objects are non keyword arguments, they're args. 5:04 And quargs always have to come after args. 5:08 We don't wanna make that syntax error. 5:11 Q objects aren't something you'll use in every project. 5:14 For example, for searching, it's often better to use a dedicated search engine, 5:17 like elastic search, than to try and build up a solution with Q objects and 5:21 their friends. 5:24 All right, in our next video will look at a more common ORM need and 5:26 a great place to take advantage of F objects. 5:30
You need to sign up for Treehouse in order to download course files.Sign up