Django: Getting Started Transcripts
Chapter: Django ORM and Databases
Lecture: More advanced queries with '.filter()'

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Great. Let's create some books. Three books created with the ids 1, 2 and 3. Now I'll query the books, once again, I've used the all query method.
0:19 There's a lot there a little hard to read but you can see ids 1, 2, and 3 show up in the query set.
0:25 So far, you've seen the query managers all and get methods. Now let's add to your repertoire.
0:31 The filter method is kind of like get but it returns multiple matches.
0:38 The title argument to filter here says to find one or more books whose title is Hard Times, there's only one, but note that a query set comes back,
0:48 Get returns a single object, filter always returns query set. Even if that query set has 0, 1, or more objects in it. Let's try another query.
1:00 The filter method can take objects as arguments. Here I've asked for all the books that have a foreign key equal to the Dickens author object.
1:10 Any object that has a foreign key pointing to it automatically gets another query manager created.
1:16 The query manager will be named the same thing as the object with the foreign key plus underscore set.
1:23 This is a convenient way of finding related objects directly from the first model.
1:28 Let's look at an example, remember that the book model has a foreign key pointing to author. So author gets a second query manager named book_set.
1:41 Like the objects query manager, I can call the all method on the book_set query manager. This is the equivalent of the filter on the book model
1:53 querying the author set to Dickens.


Talk Python's Mastodon Michael Kennedy's Mastodon