Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Restricting Results6:02 with Kenneth Love
We can already get records out of our database but we're kind of limited to none, one, or all of them. What if we just want some that match certain criteria?
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.
You need to sign up for Treehouse in order to download course files.Sign up