🐻 Queries & Using the Shell 🐻
Posted on May 19th, 2022
Today’s Topics
- Interacting with models in the shell
- Queries and more queries
🎯 Project: Still Working on Habit Tracker
Habit Tracker is due on Monday. You can do this! 💪 What do you need to figure out in order to finish it?
📖 Read | 📺 Watch | 🎧 Listen
- Database Design for Beginners This is a talk from RailsConf, which is focused on Ruby on Rails. Rails is a framework for building web applications in Ruby, very similiar to Django. It’s an excellent introduction to how to think about your data model – that is, the logic that drives the decisions you make about the models in your code and the structure of your database. Please watch it all the way through; even though it uses Ruby examples you should be able to get the gist.
This rest of this section is material to prep for the topic we will begin next week: building APIs.
- RESTful APIs Read up through the “Standard Methods” subsection of the section on “Methods”. You can read more if you want to but it gets pretty deep.
- Safia Abdullah, You and Me Learn All About HTTP
- Web APIs Explained By Selling Goods From Your Farm
🔖 Resources
Using the shell (the interactive REPL, not pipenv shell)
🦄 PRO TIPS
django-debug-toolbar
has a SQL panel that will show you the queries you are running in the view.- With
shell_plus
, you can see output of all the SQL queries if you run it like this:
python manage.py shell_plus --print-sql
Lookups
- Pretty Printed Video: How Model Queries Work in Django
- Pretty Printed Video: Querying One-to-Many Relationships in Django
- Django QuerySets
- Django Model Managers
- Django Queries: Retrieving Objects
- Field lookups
- Lookups that span relationships
- Lookups with Related objects
- Django Related Objects Reference
- Complex lookups with Q
- F objects
Constraints
- Django Docs: model constraints -> this gives you the general format/syntax
- Django Docs: UniqueConstraint
Aggregate & Annotate
- PrettyPrinted Video: Basics of Django Aggregations
- Pretty Printed Video: How to Use Annotate in Django
- Django docs:Aggregate & Annotate
- Django docs: Combining Aggregations with other QuerySets
- Aggregation Functions (e.g.,
Avg
,Count
,Min
,Max
)