Skip to content

3. Relationships & Queries

Our RescueScout app now keeps track of animals. Next we'll need to query the database to answer questions like "Which cats are still available to adopt?" We'll also need to store adoption applications for the animals at the shelter.

This chapter introduces relationships and queries with Django's ORM:

  • Foreign keys: Linking each adoption application to its animal with a ForeignKey, and following that link in either direction.
  • QuerySets: Filtering and ordering rows through a model's manager, chaining filters into one query, and when that query actually runs.
  • Management commands: In the exercise, we'll write our own seed command that fills our database with a few sample animals and applications, and run it with manage.py.