4. What we will talk about
1) What is ORM?
2) Django Models
3) Querying in Django
4) Advanced querying in Django
5) Working with real production data and keeping it safe
5. So lets get started.
Proprietary and Confidential
7. 1. code library that automates the transfer of data stored in relational
databases tables into objects that are more commonly used in
application code
high-level abstraction upon a relational database
write Python code instead of SQL to create, read, update and delete
data and schemas in database
Object-relational mapper
9. Django Models
1. A model is the single, definitive source of information about your data
2. maps to a single database table
3. Python class that subclasses django.db.models.Model.
4. Each attribute of the model represents a database field
5. Django gives you an automatically-generated database-access API
10. Let’s take a look at how a real model looks like
12. Querying in Django
1. a model class represents a database table
2. an instance of that class represents a particular record in the
database table
3. QuerySet represents one or more record based on the lookup
16. What happened behind scenes
• SELECT COUNT('*') AS `__count` FROM `orders_order` INNER JOIN
`entities_profileentity` ON ( `orders_order`.`owner_id` =
`entities_profileentity`.`entity_ptr_id` ) WHERE `entities_profileentity`.`email` =
‘ana@tapingo.com'
• SELECT COUNT('*') AS `__count` FROM `orders_order` INNER JOIN
`entities_profileentity` ON ( `orders_order`.`owner_id` =
`entities_profileentity`.`entity_ptr_id` ) WHERE (`entities_profileentity`.`email` =
'ana@tapingo.com' AND `orders_order`.`state` = 'COMPLETED' AND
`orders_order`.`shop_id` = 14474)
17. What if I want NOT?
SELECT COUNT('*') AS `__count` FROM `orders_order` INNER JOIN
`entities_profileentity` ON ( `orders_order`.`owner_id` =
`entities_profileentity`.`entity_ptr_id` ) WHERE (`entities_profileentity`.`email` =
'ana@tapingo.com' AND NOT (`orders_order`.`state` = 'COMPLETED' AND
`orders_order`.`shop_id` = 14474))
19. Behind the scenes
• SELECT `orders_order`.`id`, …, T5.`id`, T5.`name`, T5.`profile_photo_id`, T5.`last_update`,
T5.`create_time`, `entities_profileentity`.`entity_ptr_id`, `entities_profileentity`.`user_id`,
`entities_profileentity`.`last_name`, `entities_profileentity`.`email`,
`entities_profileentity`.`birthdate`, `entities_profileentity`.`phone_number`,
`entities_profileentity`.`business_day`, `entities_profileentity`.`trial_notified`,
`entities_profileentity`.`is_waiter` FROM `orders_order` INNER JOIN `entities_profileentity` ON (
`orders_order`.`owner_id` = `entities_profileentity`.`entity_ptr_id` ) INNER JOIN
`entities_shopentity` ON ( `orders_order`.`shop_id` = `entities_shopentity`.`entity_ptr_id` ) INNER
JOIN `entities_entity` ON ( `entities_shopentity`.`entity_ptr_id` = `entities_entity`.`id` ) INNER JOIN
`entities_entity` T5 ON ( `entities_profileentity`.`entity_ptr_id` = T5.`id` ) WHERE
((`entities_profileentity`.`email` = 'ana@tapingo.com' OR `entities_profileentity`.`email` =
'nadav@tapingo.com') AND `entities_entity`.`name` LIKE '%gables%' AND `orders_order`.`state` =
'COMPLETED') ORDER BY `orders_order`.`execution_time` DESC LIMIT 1
20. What else is possible?
1. values/values_list - retrieve a sub set of fields
2. distinct - get only distinct instances based on specific field
3. reverse - reverse the order in which a queryset’s elements are returned
4. dates - Returns a QuerySet that evaluates to a list of datetime.date objects representing
all available dates of a particular kind within the contents of the QuerySet
23. Behind the scenes
SELECT `entities_entity`.`id`, `entities_entity`.`name`, `entities_shopentity`.`what_now_html_takeaway_id`,
`entities_shopentity`.`meal_hack`, `entities_shopentity`.`delivery_hack`, `entities_shopentity`.`dats`,
`entities_shopentity`.`mark_new_until`, `entities_shopentity`.`mark_new_service_type`,
`entities_shopentity`.`launch_date`, `entities_shopentity`.`target_orders`, `entities_shopentity`.`odd_pos_id`,
`entities_shopentity`.`for_waiters`, `entities_shopentity`.`is_partner`,
`entities_shopentity`.`is_supports_delivery`, `entities_shopentity`.`responsibilities`, COUNT(`orders_order`.`id`)
AS `num_orders` FROM `entities_shopentity` LEFT OUTER JOIN `orders_order` ON (
`entities_shopentity`.`entity_ptr_id` = `orders_order`.`shop_id` ) INNER JOIN `entities_entity` ON (
`entities_shopentity`.`entity_ptr_id` = `entities_entity`.`id` ) WHERE NOT (`entities_shopentity`.`is_deleted` = 1)
GROUP BY `entities_shopentity`.`entity_ptr_id` ORDER BY `num_orders` ASC LIMIT 1
25. Behind the scenes
SELECT AVG(`num_shops`) FROM (SELECT `billi_organization`.`id` AS Col1,
COUNT(`entities_shopentity`.`entity_ptr_id`) AS `num_shops` FROM `billi_organization` LEFT OUTER JOIN
`entities_shopentity` ON ( `billi_organization`.`id` = `entities_shopentity`.`organization_id` ) GROUP BY
`billi_organization`.`id` ORDER BY NULL) subquery
SELECT MAX(`num_shops`) FROM (SELECT `billi_organization`.`id` AS Col1,
COUNT(`entities_shopentity`.`entity_ptr_id`) AS `num_shops` FROM `billi_organization` LEFT OUTER JOIN
`entities_shopentity` ON ( `billi_organization`.`id` = `entities_shopentity`.`organization_id` ) GROUP BY
`billi_organization`.`id` ORDER BY NULL) subquery
Introduce yourself and talk about why you are so excited to be part of the Tapingo team – a team that’s solving one of the most fundamental problems today: the way we buy things. Go around the room and talk about each person’s hopes and issues.
Present this as a “we all know” not as “we’re telling you something you don’t know.” People should be nodding in agreement here.
Like in any field, competition is a stone’s throw away. Or closer.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.
Give examples of each. Spotify – no need to buy full albums, listen to exactly what you want. Uber – convenience is power. Amazon – no need to go to the store. UPS – even old-school companies leverage technology. You can track your package like never before.