4. Lindy's Restaurant at Broadway and 52nd Street in NYC https://en.wikipedia.org/wiki/Lindy_e
ff
ect
5.
6. Tools used to inspect SQL:
1. Built-in: MyModel.objects.explain()
2. Built-in: print(MyModel.objects.all().query)
3. django-extensions: shell_plus —print-sql
4. Django debug toolbar
7. Additional requirement:
As a customer I want to see:
- a (V) next to vegetarian pizza, and
- a (VG) next to vegan pizza.
Classifying a Pizza as (VG) or (V):
A Pizza is vegan (VG) if all its Toppings are vegan.
A Pizza is vegetarian (V) if all its Toppings are vegan or vegetarian.
18. Is this a case of "Premature …" ? 🤔
Left Outer Joins .. 😱
19. Shouldn’t we be able to get the "max" topping rating with one query* on the "through" table if we know the Pizza id?
* I mean, one query without LEFT OUTER JOINS 😅
20. How can we use this aggregate query within our main query to fetch Pizzas?
How do we add max_rating to our Pizzas queryset?
* And avoid the LEFT OUTER JOIN :))
27. The constructs Django provides
What did we just do?
• Relational modeling taking querying into consideration
• Model managers to achieve DRY in querying
• Django ORM: queryset prefetch_related and select_related
• Django ORM: queryset conditional expressions (Case..when)
• Django ORM: queryset aggregation
• Django ORM: Subquery and OuterRef classes
• Django ORM: querying the M:N "through" table directly
• Unit tests: assertNumQueries + allowing us to tweak SQL while still testing logic
Together all these
• control the SQL generated by the ORM (RawSQL as a last resort)
• allow DRY
• allow unit testing all business logic within Django project