[ { "text": "[MUSIC PLAYING]", "start": 0.0, "duration": 4.419 }, { "text": "BRIAN YU: All right,\nwelcome back everyone", "start": 17.72, "duration": 1.75 }, { "text": "to Web Programming with\nPython and JavaScript.", "start": 19.47, "duration": 2.28 }, { "text": "So last time we took a look\nat a brand new web framework", "start": 21.75, "duration": 2.88 }, { "text": "written in Python called Django.", "start": 24.63, "duration": 2.01 }, { "text": "And what Django allowed us to do was\nto build dynamic web applications.", "start": 26.64, "duration": 3.63 }, { "text": "Applications where we\nnow had the ability", "start": 30.27, "duration": 2.31 }, { "text": "to not just display the same HTML and\nCSS to the user every single time,", "start": 32.58, "duration": 3.96 }, { "text": "but to dynamically\ngenerate HTML in order", "start": 36.54, "duration": 2.37 }, { "text": "to allow users to interact with a page\nthat was a little bit more dynamic.", "start": 38.91, "duration": 3.36 }, { "text": "Doing things like rendering things on\nthe page only if a certain condition", "start": 42.27, "duration": 3.45 }, { "text": "was true.", "start": 45.72, "duration": 0.78 }, { "text": "Or looping over a list of values\nand displaying one HTML element", "start": 46.5, "duration": 4.17 }, { "text": "for each of those possible values.", "start": 50.67, "duration": 1.83 }, { "text": "But where Django gets\nespecially powerful", "start": 52.5, "duration": 1.928 }, { "text": "and we're web application\ncertainly get much more interesting", "start": 54.428, "duration": 2.542 }, { "text": "is as we start to delve\ninto the world of data", "start": 56.97, "duration": 2.67 }, { "text": "and trying to have web applications\nthat store data inside of a database.", "start": 59.64, "duration": 3.832 }, { "text": "And to do that we're going to\nintroduce a couple of topics today.", "start": 63.472, "duration": 2.708 }, { "text": "In particular, we're going to\nintroduce the idea of SQL, models,", "start": 66.18, "duration": 3.24 }, { "text": "and migrations.", "start": 69.42, "duration": 1.17 }, { "text": "SQL is a database language that we\ncan use to interact with databases", "start": 70.59, "duration": 4.05 }, { "text": "and Django is going to allow us to have\nan abstraction layer on top of SQL.", "start": 74.64, "duration": 4.35 }, { "text": "To interact not by writing\ndirect SQL queries,", "start": 78.99, "duration": 2.64 }, { "text": "but by interacting with\nPython classes and objects", "start": 81.63, "duration": 2.85 }, { "text": "that we're going to refer to as models.", "start": 84.48, "duration": 2.002 }, { "text": "And migrations are going\nto be a technique that", "start": 86.482, "duration": 1.958 }, { "text": "are going to allow us to update\nour database in response to changes", "start": 88.44, "duration": 3.81 }, { "text": "that we make to our underlying models.", "start": 92.25, "duration": 2.74 }, { "text": "So before we get into things\nthat are more Django specific,", "start": 94.99, "duration": 2.57 }, { "text": "let's begin with a discussion\nof SQL more generally.", "start": 97.56, "duration": 2.46 }, { "text": "And more broadly, just a discussion\nof data and the types of data", "start": 100.02, "duration": 3.03 }, { "text": "that we're going to want to store.", "start": 103.05, "duration": 1.56 }, { "text": "And now there are a number of ways\nthat we could try to store data inside", "start": 104.61, "duration": 3.3 }, { "text": "of a computer system but\noftentimes in databases,", "start": 107.91, "duration": 2.82 }, { "text": "in particular, in a type of database\nknown as a relational database,", "start": 110.73, "duration": 3.6 }, { "text": "we're going to store data inside\nof a table where each table is", "start": 114.33, "duration": 3.36 }, { "text": "going to have rows and columns.", "start": 117.69, "duration": 2.35 }, { "text": "So for the sake of today,\nwe're going to imagine", "start": 120.04, "duration": 2.0 }, { "text": "starting to construct a\ndatabase and a web application", "start": 122.04, "duration": 2.73 }, { "text": "that an airline might use.", "start": 124.77, "duration": 1.142 }, { "text": "For example, for keeping track\nof various different flights", "start": 125.912, "duration": 2.458 }, { "text": "on that airline and keeping track of\nwhich passengers are on those flights.", "start": 128.37, "duration": 4.14 }, { "text": "And so what we have here is\na sample table, for example,", "start": 132.51, "duration": 3.15 }, { "text": "of how you might represent a whole\nbunch of flight related data.", "start": 135.66, "duration": 3.6 }, { "text": "Here I have three columns for\nthree pieces of information", "start": 139.26, "duration": 3.15 }, { "text": "that I might want to keep track\nof for any particular flight.", "start": 142.41, "duration": 2.73 }, { "text": "For any given flight I might\nwant to know that flight's", "start": 145.14, "duration": 2.292 }, { "text": "origin, what city it's weaving from.", "start": 147.432, "duration": 1.578 }, { "text": "Its destination, what\ncity it's arriving at.", "start": 149.01, "duration": 2.37 }, { "text": "And its duration, some\nvalue in minutes of how long", "start": 151.38, "duration": 3.0 }, { "text": "it's going to take the flight to get\nfrom the origin to the destination.", "start": 154.38, "duration": 3.79 }, { "text": "So each of those columns\nrepresents one field", "start": 158.17, "duration": 2.18 }, { "text": "that I might want to keep\ntrack of about my data.", "start": 160.35, "duration": 2.34 }, { "text": "And each row, meanwhile, is going\nto represent an individual flight.", "start": 162.69, "duration": 3.81 }, { "text": "So here, for example, is one row\nrepresenting one flight from New York", "start": 166.5, "duration": 3.78 }, { "text": "to London and that flight just\nso happens to take 415 minutes.", "start": 170.28, "duration": 5.01 }, { "text": "And so what SQL is\ngoing to allow us to do", "start": 175.29, "duration": 2.04 }, { "text": "is it is going to be a\ndatabase of language that", "start": 177.33, "duration": 2.49 }, { "text": "is designed to interact with\nrelational database management systems.", "start": 179.82, "duration": 3.84 }, { "text": "Databases that organize data into tables\nwhere every table has rows and columns.", "start": 183.66, "duration": 4.95 }, { "text": "And it turns out, there are a number of\ndifferent database management systems", "start": 188.61, "duration": 3.21 }, { "text": "that implement parts of the SQL\nstandard of this type of language.", "start": 191.82, "duration": 3.36 }, { "text": "Some of the more popular are\nMySQL, PostgreSQL, and SQLite", "start": 195.18, "duration": 3.84 }, { "text": "but there are definitely others as well.", "start": 199.02, "duration": 1.667 }, { "text": "And each of these has various different\nfeatures and different use cases", "start": 200.687, "duration": 3.0 }, { "text": "where some might be more appropriate.", "start": 203.687, "duration": 1.653 }, { "text": "MySQL and PostgreSQL, otherwise\nknown as just Postgres,", "start": 205.34, "duration": 3.88 }, { "text": "are more heavier database\nmanagement systems, so to speak.", "start": 209.22, "duration": 3.63 }, { "text": "They're generally running on servers\nelsewhere where a lot of big companies", "start": 212.85, "duration": 4.11 }, { "text": "will use servers like this\nin order to store data", "start": 216.96, "duration": 2.613 }, { "text": "and represent their data in a\nsort of a separate database where.", "start": 219.573, "duration": 2.667 }, { "text": "The idea of that is that you might\nwant your web application running", "start": 222.24, "duration": 2.88 }, { "text": "on one server but you\nmight want your database", "start": 225.12, "duration": 1.933 }, { "text": "running as an entirely separate process.", "start": 227.053, "duration": 1.667 }, { "text": "Such that they can behave independently,\nyou can debug and test them", "start": 228.72, "duration": 2.97 }, { "text": "independently, and if one goes down,\nthe other doesn't go down as well.", "start": 231.69, "duration": 4.32 }, { "text": "SQLite is a bit of a\nsimpler lighter weight", "start": 236.01, "duration": 2.43 }, { "text": "implementation of the SQL standard.", "start": 238.44, "duration": 1.98 }, { "text": "And what SQLite is going to do is\nrather than be an entire server that", "start": 240.42, "duration": 3.41 }, { "text": "is going to be listening for\nrequests and making responses,", "start": 243.83, "duration": 2.77 }, { "text": "SQLite is just going to store\nall of its data as a single file.", "start": 246.6, "duration": 3.55 }, { "text": "So we're going to store SQL\ndata inside of a SQLite file", "start": 250.15, "duration": 2.48 }, { "text": "and that's going to make it\na little bit easier for us", "start": 252.63, "duration": 2.25 }, { "text": "as we're just getting started to\nbegin to write queries and add things", "start": 254.88, "duration": 3.3 }, { "text": "to our database and retrieve\nthings from our database.", "start": 258.18, "duration": 2.43 }, { "text": "But know that many of the same types of\nSQL syntax that we'll see with SQLite,", "start": 260.61, "duration": 3.54 }, { "text": "which is Django's default,\ncan also be applied", "start": 264.15, "duration": 2.58 }, { "text": "to other languages as well and other\ndatabase management systems too.", "start": 266.73, "duration": 4.32 }, { "text": "So as we begin to store data\ninside of a SQL database,", "start": 271.05, "duration": 3.12 }, { "text": "ultimately each of those\npieces of data has a type.", "start": 274.17, "duration": 2.997 }, { "text": "In the same way that\nin Python we have types", "start": 277.167, "duration": 1.833 }, { "text": "for various different types of data,\nwe have integers and we have strings", "start": 279.0, "duration": 3.33 }, { "text": "and we have lists and\ntuples and so forth.", "start": 282.33, "duration": 2.04 }, { "text": "SQL too has types that represent various\ndifferent categories of information", "start": 284.37, "duration": 4.8 }, { "text": "that you might want to\nstore inside of a database.", "start": 289.17, "duration": 2.2 }, { "text": "And each database management system\nhas its own different set of types.", "start": 291.37, "duration": 3.29 }, { "text": "SQLite for example, has a fairly short\nlist of basic types that it supports.", "start": 294.66, "duration": 4.92 }, { "text": "It supports text for just like\nstrings of text, for example.", "start": 299.58, "duration": 3.66 }, { "text": "Something like a city name, for\ninstance, might be stored as text.", "start": 303.24, "duration": 3.12 }, { "text": "We have support for integers,\nwhich are just 0, 1, 2, 3 4", "start": 306.36, "duration": 3.3 }, { "text": "plus the negative numbers, negative 1,\nnegative 2, negative 3, and so forth.", "start": 309.66, "duration": 4.45 }, { "text": "We have support for real\nnumbers where real numbers don't", "start": 314.11, "duration": 2.48 }, { "text": "have to just be integers, they can\nhave something after the decimal point.", "start": 316.59, "duration": 3.29 }, { "text": "Something like 2.8 or something else\nthat might have a decimal in it.", "start": 319.88, "duration": 3.51 }, { "text": "And then numeric is another category\nfor data that's just more generally", "start": 323.39, "duration": 3.57 }, { "text": "some sort of number that\nmight number like data,", "start": 326.96, "duration": 2.41 }, { "text": "so something like a Boolean value or\nsomething like a date, for example,", "start": 329.37, "duration": 3.98 }, { "text": "might be stored using this numeric type.", "start": 333.35, "duration": 1.68 }, { "text": "Where it's not really an\ninteger or a real number,", "start": 335.03, "duration": 2.083 }, { "text": "though you could represented at such,\nit's other numeric type of data.", "start": 337.113, "duration": 3.987 }, { "text": "And then finally blob, which\nstands for binary large object,", "start": 341.1, "duration": 3.41 }, { "text": "is any type of other binary data.", "start": 344.51, "duration": 1.86 }, { "text": "Just zeros and ones that you might\nwant to store inside of your database.", "start": 346.37, "duration": 3.61 }, { "text": "So for example, if you're\nstoring files, maybe they're", "start": 349.98, "duration": 2.27 }, { "text": "audio files or images\nor other types of files", "start": 352.25, "duration": 2.43 }, { "text": "that you want to keep track\nof inside of your database,", "start": 354.68, "duration": 2.62 }, { "text": "you can store just pure binary data\ninside of a SQL database as well.", "start": 357.3, "duration": 4.71 }, { "text": "So these now are the basic\ntypes of SQLite supports", "start": 362.01, "duration": 3.02 }, { "text": "but other database\nmanagement systems have", "start": 365.03, "duration": 1.83 }, { "text": "other types that they support as well.", "start": 366.86, "duration": 1.59 }, { "text": "MySQL, you all for example, has\na much longer list of other types", "start": 368.45, "duration": 3.24 }, { "text": "that it supports.", "start": 371.69, "duration": 1.08 }, { "text": "In addition to just supporting a\ntext type for arbitrary length text,", "start": 372.77, "duration": 3.57 }, { "text": "MySQL also has a type called char\nthat takes an argument of size.", "start": 376.34, "duration": 4.41 }, { "text": "For saying I want like size\ncharacters worth of data", "start": 380.75, "duration": 3.12 }, { "text": "that we're going to store\ninside the database.", "start": 383.87, "duration": 1.92 }, { "text": "And you might imagine that this\ncan be advantageous in situations", "start": 385.79, "duration": 3.06 }, { "text": "where you know that\nyour data is going to be", "start": 388.85, "duration": 1.92 }, { "text": "within a certain number of characters.", "start": 390.77, "duration": 1.68 }, { "text": "So if you're storing a zip code\nfor example, in the United States.", "start": 392.45, "duration": 3.24 }, { "text": "A zip code in the United States\nmight just be five characters", "start": 395.69, "duration": 2.97 }, { "text": "and so you can allocate\na character length", "start": 398.66, "duration": 1.77 }, { "text": "of five characters worth of space\nfor storing every single zip code", "start": 400.43, "duration": 3.81 }, { "text": "inside of your database table.", "start": 404.24, "duration": 1.385 }, { "text": "So for efficiency's sake, if\nyou know the maximum length,", "start": 405.625, "duration": 2.755 }, { "text": "and you can fix that length\ninside your database,", "start": 408.38, "duration": 2.61 }, { "text": "that can be an efficient choice.", "start": 410.99, "duration": 1.59 }, { "text": "VARCHAR of a size is similar.", "start": 412.58, "duration": 2.07 }, { "text": "And this is a variable length character.", "start": 414.65, "duration": 2.07 }, { "text": "If something isn't going to be exactly\na certain number of characters,", "start": 416.72, "duration": 3.0 }, { "text": "but maybe up to a certain\nnumber of characters,", "start": 419.72, "duration": 2.34 }, { "text": "you can use VARCHAR to\nsay maybe sometimes it's", "start": 422.06, "duration": 2.1 }, { "text": "going to be fewer characters.", "start": 424.16, "duration": 1.47 }, { "text": "But it could be up to\nsize, number of characters.", "start": 425.63, "duration": 3.21 }, { "text": "And there are tradeoffs there, too.", "start": 428.84, "duration": 2.175 }, { "text": "Then there are various\ndifferent types of integers.", "start": 431.015, "duration": 2.125 }, { "text": "There's not just a single integer type.", "start": 433.14, "duration": 1.92 }, { "text": "But SMALLINT and INT and BIGINT, each\nof which uses a different number of", "start": 435.06, "duration": 3.65 }, { "text": "bytes in order to store an integer data.", "start": 438.71, "duration": 2.29 }, { "text": "So if you want to be able to store\nmuch larger or much smaller numbers,", "start": 441.0, "duration": 3.17 }, { "text": "you might need a bigger type,\nlike an INT or a BIGINT.", "start": 444.17, "duration": 2.278 }, { "text": "But if your numbers are\ngoing to be pretty small,", "start": 446.448, "duration": 2.042 }, { "text": "maybe you're OK with just a SMALLINT.", "start": 448.49, "duration": 2.0 }, { "text": "And likewise there are similar types of\ntrade-offs for floating point numbers.", "start": 450.49, "duration": 3.25 }, { "text": "Where, much as in programming\nlanguages like C, if you're familiar,", "start": 453.74, "duration": 2.792 }, { "text": "have both a float and a double type.", "start": 456.532, "duration": 1.888 }, { "text": "Where a double uses more bytes of\nmemory in order to store information.", "start": 458.42, "duration": 3.66 }, { "text": "Here, too, we have FLOAT\nand DOUBLE, which allow", "start": 462.08, "duration": 2.7 }, { "text": "us to store floating point numbers.", "start": 464.78, "duration": 1.63 }, { "text": "Numbers that might be real numbered\nvalues, where a double just", "start": 466.41, "duration": 2.99 }, { "text": "allows us to express a number\nto a little bit more precision", "start": 469.4, "duration": 3.06 }, { "text": "than a floating point\nnumber might be able to.", "start": 472.46, "duration": 2.07 }, { "text": "And there are many other types\nas well, but the general idea", "start": 474.53, "duration": 2.5 }, { "text": "is that here even in SQL,\nmuch as in a language", "start": 477.03, "duration": 2.3 }, { "text": "like Python, we have types for each of\nthese various different kinds of data.", "start": 479.33, "duration": 4.66 }, { "text": "So now that we understand that each type\nof data has types, what we'd like to do", "start": 483.99, "duration": 3.65 }, { "text": "is to be able to, inside of a\ndatabase, actually create a table.", "start": 487.64, "duration": 3.777 }, { "text": "And there are various\ndifferent SQL commands", "start": 491.417, "duration": 1.833 }, { "text": "that we can run on our\ndatabase in order to perform", "start": 493.25, "duration": 2.58 }, { "text": "various different operations.", "start": 495.83, "duration": 1.59 }, { "text": "And the first we might\nwant to execute is", "start": 497.42, "duration": 1.83 }, { "text": "a command that is just going\nto create a table, for example.", "start": 499.25, "duration": 3.28 }, { "text": "So how might we go about doing that?", "start": 502.53, "duration": 1.62 }, { "text": "Well, it turns out the\nsyntax for creating", "start": 504.15, "duration": 1.75 }, { "text": "a table is going to look a\nlittle something like this.", "start": 505.9, "duration": 2.56 }, { "text": "This here is the syntax we would\nuse to create a table in SQLite", "start": 508.46, "duration": 4.17 }, { "text": "for representing flights, for example.", "start": 512.63, "duration": 2.789 }, { "text": "So we begin with the\nkeyword Create Table,", "start": 515.419, "duration": 2.88 }, { "text": "to say I would like to create a\nnew table inside of this database.", "start": 518.299, "duration": 3.63 }, { "text": "Next I give the name of the table.", "start": 521.929, "duration": 1.921 }, { "text": "Every table has a name\njust for ease of reference.", "start": 523.85, "duration": 2.43 }, { "text": "And here the name and the table\nis just the word \"flights.\"", "start": 526.28, "duration": 3.6 }, { "text": "Then what follows in parentheses\nare a comma-separated list", "start": 529.88, "duration": 3.87 }, { "text": "of all of the columns that\nshould be present in the table.", "start": 533.75, "duration": 3.09 }, { "text": "I'm not yet adding\nany data to the table.", "start": 536.84, "duration": 2.22 }, { "text": "The Create Table query is just going\nto decide the structure of the table.", "start": 539.06, "duration": 3.79 }, { "text": "What are the columns and\nwhat are the columns' types.", "start": 542.85, "duration": 3.1 }, { "text": "And so here we see that\nin the beginning of each", "start": 545.95, "duration": 2.32 }, { "text": "of these clauses that are separated by\ncommas, we have the name of the column.", "start": 548.27, "duration": 5.62 }, { "text": "So we have an id column because, though\nwe didn't see this before, often in SQL", "start": 553.89, "duration": 4.13 }, { "text": "it's going to be helpful\nto be able to have", "start": 558.02, "duration": 1.89 }, { "text": "some way of uniquely referencing a\nparticular element inside of the table.", "start": 559.91, "duration": 3.902 }, { "text": "Maybe there are multiple\nflights that are", "start": 563.812, "duration": 1.708 }, { "text": "going from New York to\nLondon, for example,", "start": 565.52, "duration": 3.45 }, { "text": "and I want some way of\ndistinguishing between them.", "start": 568.97, "duration": 2.31 }, { "text": "And one easy way is to give every\nsingle entry inside of a table", "start": 571.28, "duration": 3.93 }, { "text": "a unique identifier, or a unique id,\nrepresented here by this id column.", "start": 575.21, "duration": 4.827 }, { "text": "And that's going to be a way of\nmaking sure that we can always", "start": 580.037, "duration": 2.583 }, { "text": "access a particular flight uniquely.", "start": 582.62, "duration": 2.94 }, { "text": "So we have an id column, an origin\ncolumn, a destination column,", "start": 585.56, "duration": 3.27 }, { "text": "and a duration column.", "start": 588.83, "duration": 1.65 }, { "text": "Each of those columns has a type.", "start": 590.48, "duration": 2.79 }, { "text": "So the types here, id is an integer.", "start": 593.27, "duration": 2.01 }, { "text": "It's just going to be some number\nrepresenting the flight, counting one,", "start": 595.28, "duration": 3.0 }, { "text": "two, three, four, and\nso on and so forth.", "start": 598.28, "duration": 2.22 }, { "text": "Origin and destination\nwe'll label here as text.", "start": 600.5, "duration": 2.87 }, { "text": "Though you'll see that\nif we were using my SQL,", "start": 603.37, "duration": 2.65 }, { "text": "I might make these a variable\nlength character that", "start": 606.02, "duration": 2.305 }, { "text": "could be up to a certain\nnumber of characters", "start": 608.325, "duration": 1.875 }, { "text": "if I know there's\nprobably not going to be", "start": 610.2, "duration": 1.75 }, { "text": "a city that's longer than a certain\nnumber of characters, for instance.", "start": 611.95, "duration": 3.4 }, { "text": "Then I have a duration,\nwhich here is an integer.", "start": 615.35, "duration": 2.72 }, { "text": "Some number of minutes\nthat is going to be", "start": 618.07, "duration": 1.83 }, { "text": "how long it takes for this flight to\ngo from the origin to the destination.", "start": 619.9, "duration": 4.82 }, { "text": "After the types for\neach of the columns, I", "start": 624.72, "duration": 1.75 }, { "text": "have additional constraints that I\nmight add to the columns as well.", "start": 626.47, "duration": 3.48 }, { "text": "So integer Primary Key.", "start": 629.95, "duration": 1.98 }, { "text": "The Primary Key here\nmeans that the id is", "start": 631.93, "duration": 2.37 }, { "text": "going to be the primary way via\nwhich I uniquely identify a flight.", "start": 634.3, "duration": 4.02 }, { "text": "There's going to be\nsome optimizations here", "start": 638.32, "duration": 1.8 }, { "text": "to make it very quick to be able\nto look up a flight by its id.", "start": 640.12, "duration": 4.26 }, { "text": "Null is a constraint that I can\nplace on columns to be able to say,", "start": 644.38, "duration": 3.91 }, { "text": "I don't want to allow for this\nparticular column to ever be empty.", "start": 648.29, "duration": 3.39 }, { "text": "I don't want there to ever be a\nflight that doesn't have an origin", "start": 651.68, "duration": 2.75 }, { "text": "or doesn't have a\ndestination or a duration.", "start": 654.43, "duration": 2.38 }, { "text": "So I can add constraints to\na particular column to say,", "start": 656.81, "duration": 3.65 }, { "text": "the origin, destination,\nduration, columns", "start": 660.46, "duration": 2.49 }, { "text": "should not be allowed to be null.", "start": 662.95, "duration": 1.74 }, { "text": "I want to always have an origin\nand a destination and a duration.", "start": 664.69, "duration": 4.53 }, { "text": "Finally this Autoincrement\nafter the Primary Key", "start": 669.22, "duration": 2.88 }, { "text": "is a cue into SQL to automatically\nupdate the id every time", "start": 672.1, "duration": 3.87 }, { "text": "I add a new row into the table.", "start": 675.97, "duration": 1.612 }, { "text": "So if I add a new flight,\nI can give the flight", "start": 677.582, "duration": 1.958 }, { "text": "an origin, destination, and duration.", "start": 679.54, "duration": 2.01 }, { "text": "But I don't have to worry\nabout giving it an id.", "start": 681.55, "duration": 2.338 }, { "text": "SQL is going to handle\nthat for me automatically.", "start": 683.888, "duration": 2.042 }, { "text": "Automatically giving that new row\nin that table the next available id", "start": 685.93, "duration": 6.75 }, { "text": "number.", "start": 692.68, "duration": 1.05 }, { "text": "Here, then, is the way we create a\ntable by giving the name of the table,", "start": 693.73, "duration": 3.3 }, { "text": "each of the columns, the types\nfor each of those columns,", "start": 697.03, "duration": 2.91 }, { "text": "and then any constraints we would\nwant to place on the columns.", "start": 699.94, "duration": 4.45 }, { "text": "There are a number of\ndifferent types of constraints", "start": 704.39, "duration": 2.3 }, { "text": "that I can add to a particular column.", "start": 706.69, "duration": 1.68 }, { "text": "Not null is one we've seen before.", "start": 708.37, "duration": 1.77 }, { "text": "You can add a default, which just adds\na default value to a particular column.", "start": 710.14, "duration": 4.65 }, { "text": "Primary Key we've seen.", "start": 714.79, "duration": 1.26 }, { "text": "Unique guarantees that every\nvalue is going to be unique.", "start": 716.05, "duration": 3.18 }, { "text": "So if you don't want to\nallow for the same value", "start": 719.23, "duration": 2.61 }, { "text": "to appear twice for a\nparticular column, you", "start": 721.84, "duration": 2.417 }, { "text": "can add a unique constraint\nto a particular column", "start": 724.257, "duration": 2.083 }, { "text": "to enforce that as well.", "start": 726.34, "duration": 1.86 }, { "text": "Check can be used to make sure that\na value obeys a certain condition.", "start": 728.2, "duration": 3.96 }, { "text": "Like, a number falls within\na certain range, for example.", "start": 732.16, "duration": 2.738 }, { "text": "If you have movie ratings, for example,\nand you're storing movie ratings inside", "start": 734.898, "duration": 3.292 }, { "text": "of a database, you\nmight care to make sure", "start": 738.19, "duration": 1.8 }, { "text": "that those ratings are within the\n1 to 5 range or the 1 to 10 range", "start": 739.99, "duration": 3.9 }, { "text": "or whatever range you deem\nvalid for the database.", "start": 743.89, "duration": 3.3 }, { "text": "So via constraints you\ncan begin to ensure", "start": 747.19, "duration": 2.31 }, { "text": "that as you add data to the table, that\ndata is going to be valid in some way.", "start": 749.5, "duration": 3.96 }, { "text": "That it needs to obey\nand certain constraints,", "start": 753.46, "duration": 2.67 }, { "text": "otherwise, if you try\nto add it to the table,", "start": 756.13, "duration": 2.11 }, { "text": "it's not going to be accepted at all.", "start": 758.24, "duration": 2.66 }, { "text": "And so that leads into the\nnatural next question, which", "start": 760.9, "duration": 2.4 }, { "text": "is, I now have all of these\nvarious different tables", "start": 763.3, "duration": 2.64 }, { "text": "that I can create using\nthese create table commands,", "start": 765.94, "duration": 2.49 }, { "text": "but how do I actually add\ndata into those tables?", "start": 768.43, "duration": 3.24 }, { "text": "The way we're going to do that is\nvia an INSERT command, where we're", "start": 771.67, "duration": 3.36 }, { "text": "going to insert data into a SQL table.", "start": 775.03, "duration": 3.36 }, { "text": "So what might that command look like?", "start": 778.39, "duration": 1.62 }, { "text": "The command is generally\ngoing to look like this.", "start": 780.01, "duration": 2.64 }, { "text": "We're going to say,\nINSERT Into, to say I", "start": 782.65, "duration": 2.49 }, { "text": "would like to add a\nnew row into a table.", "start": 785.14, "duration": 3.03 }, { "text": "Following that, we provide\nthe name of the table.", "start": 788.17, "duration": 2.07 }, { "text": "Something like \"flight\"\nto say, I would like", "start": 790.24, "duration": 1.833 }, { "text": "to add a new row to the flights table.", "start": 792.073, "duration": 2.577 }, { "text": "And following the name\nof the table, we need", "start": 794.65, "duration": 2.01 }, { "text": "to provide in parentheses\na comma-separated list", "start": 796.66, "duration": 3.24 }, { "text": "of all of the column names for which\nwe are going to provide values.", "start": 799.9, "duration": 4.93 }, { "text": "So here I said I want to provide\nvalues for the origin, destination,", "start": 804.83, "duration": 3.84 }, { "text": "and duration of this flight.", "start": 808.67, "duration": 2.06 }, { "text": "Note that I'm leaving out the ID column.", "start": 810.73, "duration": 1.8 }, { "text": "I don't have to worry about\nadding a value for the ID,", "start": 812.53, "duration": 2.588 }, { "text": "because I said the id\nshould autoincrement.", "start": 815.118, "duration": 1.792 }, { "text": "So it's going to automatically\ngive the next available ID.", "start": 816.91, "duration": 3.54 }, { "text": "And what values am I going to provide?", "start": 820.45, "duration": 1.89 }, { "text": "Well, after the word\n\"values\", I provide, again,", "start": 822.34, "duration": 2.56 }, { "text": "in parentheses, a comma-separated\nlist of all of the values", "start": 824.9, "duration": 3.77 }, { "text": "that are to be associated with\nthose columns in the same order.", "start": 828.67, "duration": 3.3 }, { "text": "So origin was the first one here.", "start": 831.97, "duration": 1.9 }, { "text": "So origin will correspond to New York.", "start": 833.87, "duration": 1.94 }, { "text": "Destination will correspond to London.", "start": 835.81, "duration": 2.04 }, { "text": "Duration will correspond to 415.", "start": 837.85, "duration": 3.06 }, { "text": "So this query let's me take the\nflights table that I've already created", "start": 840.91, "duration": 3.84 }, { "text": "and add some data into that table.", "start": 844.75, "duration": 4.26 }, { "text": "So you can use these\nINSERT queries once you", "start": 849.01, "duration": 2.28 }, { "text": "have a table to being able to add\nnew rows of data to that table.", "start": 851.29, "duration": 3.18 }, { "text": "Every time a new flight\ncomes up for an airline,", "start": 854.47, "duration": 2.31 }, { "text": "the airline might, behind the\nscenes, be running an INSERT", "start": 856.78, "duration": 2.79 }, { "text": "into Flights command that\ntakes data about that flight", "start": 859.57, "duration": 3.24 }, { "text": "and adds it to the table.", "start": 862.81, "duration": 1.53 }, { "text": "But, of course, once we've\nadded data to a table,", "start": 864.34, "duration": 2.58 }, { "text": "we'd ideally like some way to get\ndata out of that table, as well.", "start": 866.92, "duration": 3.86 }, { "text": "That once we've stored\ndata inside of our database", "start": 870.78, "duration": 2.44 }, { "text": "we want to retrieve that data, too.", "start": 873.22, "duration": 2.13 }, { "text": "To do that, we're going to\nuse a particular type of query", "start": 875.35, "duration": 2.94 }, { "text": "called a SELECT query.", "start": 878.29, "duration": 1.47 }, { "text": "What SELECT is going to do is\ntake a table that already exists", "start": 879.76, "duration": 3.93 }, { "text": "and allow us to get\ndata out of that table.", "start": 883.69, "duration": 2.992 }, { "text": "It's not going to modify\nthe data that's there,", "start": 886.682, "duration": 1.958 }, { "text": "it's just going to retrieve data that\nmight be inside a particular table.", "start": 888.64, "duration": 3.702 }, { "text": "So what do those queries look like?", "start": 892.342, "duration": 1.458 }, { "text": "Well, the simplest query might\nlook a little something like this.", "start": 893.8, "duration": 2.708 }, { "text": "SELECT * from flights.", "start": 896.508, "duration": 2.632 }, { "text": "SELECT from flights means I want to\nselect data from the flights table.", "start": 899.14, "duration": 4.44 }, { "text": "And this * is a wild\ncard that here just means", "start": 903.58, "duration": 3.42 }, { "text": "select all of the possible\ncolumns that you can.", "start": 907.0, "duration": 2.56 }, { "text": "So if this is my table,\nmy table of flights", "start": 910.06, "duration": 1.86 }, { "text": "where each flight has an id, an\norigin, a destination, and a duration,", "start": 911.92, "duration": 5.05 }, { "text": "then select * from flights\nwill select all of that data.", "start": 916.97, "duration": 3.12 }, { "text": "All of the rows and\nall of the columns are", "start": 920.09, "duration": 2.22 }, { "text": "going to come back to me\nwhen I run a query like this.", "start": 922.31, "duration": 3.638 }, { "text": "Now it's possible that when I'm running\nthese queries, when I'm accessing data,", "start": 925.948, "duration": 3.292 }, { "text": "that I don't actually care\nabout all of these columns,", "start": 929.24, "duration": 2.398 }, { "text": "especially if I have a table with\nmany more columns than just this.", "start": 931.638, "duration": 2.792 }, { "text": "Maybe I only care about\nparticular columns.", "start": 934.43, "duration": 2.73 }, { "text": "And it would be wasteful\nto query the database", "start": 937.16, "duration": 2.01 }, { "text": "asking it to return to me a whole\nlot more data than I actually need.", "start": 939.17, "duration": 4.06 }, { "text": "So instead I could run query like this.", "start": 943.23, "duration": 2.66 }, { "text": "SELECT-- instead of * I'm saying select\norigin comma destination from flights.", "start": 945.89, "duration": 6.42 }, { "text": "So I'm again selecting\nfrom the flights table.", "start": 952.31, "duration": 1.93 }, { "text": "Selecting all of the rows\nstill, but the difference", "start": 954.24, "duration": 3.26 }, { "text": "is that instead of select *, which\nmeans select all of the columns that", "start": 957.5, "duration": 3.69 }, { "text": "are present in the table, select\norigin comma destination means I'm only", "start": 961.19, "duration": 4.2 }, { "text": "going to select these two columns--", "start": 965.39, "duration": 1.98 }, { "text": "the origin column and\nthe destination column.", "start": 967.37, "duration": 2.52 }, { "text": "So highlighted in blue\nhere is the data that", "start": 969.89, "duration": 2.28 }, { "text": "would be returned if someone were\nto run this particular SQL query.", "start": 972.17, "duration": 4.65 }, { "text": "Now especially as tables begin to get\nlarger and begin to have more and more", "start": 976.82, "duration": 3.45 }, { "text": "rows-- here I just have six rows,\nbut you might imagine a table with", "start": 980.27, "duration": 3.12 }, { "text": "thousands or even millions\nof rows within it--", "start": 983.39, "duration": 2.43 }, { "text": "you probably don't want to be returning\nevery single row every single time.", "start": 985.82, "duration": 4.09 }, { "text": "So just as you can constrain\nwhich columns come back to you,", "start": 989.91, "duration": 2.79 }, { "text": "likewise, you too can\nconstrain which rows come back", "start": 992.7, "duration": 2.54 }, { "text": "to you when you perform a query.", "start": 995.24, "duration": 2.07 }, { "text": "So I could say something like\nselect * from flights where id= \"3.\"", "start": 997.31, "duration": 4.31 }, { "text": "Again, this reads as very English like.", "start": 1001.62, "duration": 2.29 }, { "text": "I'm saying select all of the\ncolumns from the flights table,", "start": 1003.91, "duration": 3.78 }, { "text": "but not all of the rows.", "start": 1007.69, "duration": 1.32 }, { "text": "I only want to select the rows where--", "start": 1009.01, "duration": 2.76 }, { "text": "this is a SQL keyword--", "start": 1011.77, "duration": 1.38 }, { "text": "where id= \"3.\"", "start": 1013.15, "duration": 1.8 }, { "text": "ID is the name of a\ncolumn, 3 is the value,", "start": 1014.95, "duration": 2.67 }, { "text": "and it's going to look through this\ntable, only find me the flights", "start": 1017.62, "duration": 2.85 }, { "text": "where the id is equal to 3, and I'll\njust get back that one row as a result.", "start": 1020.47, "duration": 4.77 }, { "text": "It finds the row with an ID of\n3, and it gives that back to me.", "start": 1025.24, "duration": 5.129 }, { "text": "If, instead, I said select * from\nflights where origin= \"New York,\"", "start": 1030.369, "duration": 3.901 }, { "text": "I can see that I don't just need\nto filter on the primary key--", "start": 1034.27, "duration": 2.79 }, { "text": "the id-- I can also filter\non any other column.", "start": 1037.06, "duration": 2.61 }, { "text": "I can say, give me all the flights\nwhere New York is the origin.", "start": 1039.67, "duration": 3.25 }, { "text": "So all the flights\nleaving from New York.", "start": 1042.92, "duration": 2.054 }, { "text": "You might imagine that\nin New York's airport,", "start": 1044.974, "duration": 1.875 }, { "text": "for example they likely want\nsome sort of query that's", "start": 1046.849, "duration": 2.721 }, { "text": "going to find all the flights\nleaving from New York such", "start": 1049.57, "duration": 2.43 }, { "text": "that they can display on a screen\nof some sort all of the departures", "start": 1052.0, "duration": 3.48 }, { "text": "from that particular airport.", "start": 1055.48, "duration": 1.29 }, { "text": "So you could run a query\nlike this and get back", "start": 1056.77, "duration": 2.28 }, { "text": "only the rows in this database, inside\nof this table, that happened to have", "start": 1059.05, "duration": 4.08 }, { "text": "an origin of New York, for example.", "start": 1063.13, "duration": 3.19 }, { "text": "So let's go ahead and take a\nlook at how we might actually", "start": 1066.32, "duration": 2.78 }, { "text": "run some of these queries\ninside of a SQL database.", "start": 1069.1, "duration": 4.21 }, { "text": "So in order to create a SQLite\ndatabase, which is really just a file,", "start": 1073.31, "duration": 3.75 }, { "text": "I can start just by creating the file.", "start": 1077.06, "duration": 2.18 }, { "text": "It turns out that in the terminal,\nthere is a command called Touch that", "start": 1079.24, "duration": 3.18 }, { "text": "will create a brand new file for me.", "start": 1082.42, "duration": 2.19 }, { "text": "So I'm going to create a\nfile called flights.sql.", "start": 1084.61, "duration": 4.04 }, { "text": "It's going to be a SQLite database\nfile that by default has nothing in it.", "start": 1088.65, "duration": 4.03 }, { "text": "But then, assuming you\nhave SQLite installed,", "start": 1092.68, "duration": 2.46 }, { "text": "I can run SQLite 3\nfollowed by flights.sql,", "start": 1095.14, "duration": 5.01 }, { "text": "and now I'm inside of the SQLite prompt.", "start": 1100.15, "duration": 2.55 }, { "text": "I can now begin to write commands that\nwill be executed on this SQL database.", "start": 1102.7, "duration": 5.53 }, { "text": "So one thing I might do is\ncreate a table called \"flights.\"", "start": 1108.23, "duration": 4.14 }, { "text": "In this table, I want to have an\nid column, which is an integer.", "start": 1112.37, "duration": 4.29 }, { "text": "It should be the primary key, the main\nway via which I identify a flight.", "start": 1116.66, "duration": 3.59 }, { "text": "I would like to autoincrement it.", "start": 1120.25, "duration": 2.643 }, { "text": "I would also like for the flights\ntable to have an origin, which", "start": 1122.893, "duration": 2.667 }, { "text": "will be a text field that is not null.", "start": 1125.56, "duration": 2.63 }, { "text": "I'll add a destination field, also\na text field that is not null.", "start": 1128.19, "duration": 4.3 }, { "text": "And then finally I'll\nadd a duration field,", "start": 1132.49, "duration": 2.16 }, { "text": "which is an integer that\nalso shouldn't be null.", "start": 1134.65, "duration": 2.76 }, { "text": "I'll end with an end parentheses\nand a semicolon, press Return,", "start": 1137.41, "duration": 3.42 }, { "text": "and that executes that\ncommand on my database.", "start": 1140.83, "duration": 2.61 }, { "text": "I have now created a\ntable called \"flights.\"", "start": 1143.44, "duration": 2.82 }, { "text": "To verify this in SQLite, in\nthe prompt I can type .tables,", "start": 1146.26, "duration": 3.89 }, { "text": "and that will show me all of the tables\nthat currently exist in my database.", "start": 1150.15, "duration": 4.06 }, { "text": "It just so happens that I\nhave a table called \"flights.\"", "start": 1154.21, "duration": 3.125 }, { "text": "Of course, there's nothing\ninside this table, which", "start": 1157.335, "duration": 2.125 }, { "text": "I can verify by running\nSELECT * from flights,", "start": 1159.46, "duration": 3.02 }, { "text": "meaning get all the data\nfrom the flights table.", "start": 1162.48, "duration": 2.65 }, { "text": "If I press Return nothing happens.", "start": 1165.13, "duration": 2.01 }, { "text": "I don't see anything.", "start": 1167.14, "duration": 1.05 }, { "text": "There is no data that\nhappens to be here.", "start": 1168.19, "duration": 2.55 }, { "text": "But now I could say something\nlike INSERT into Flights.", "start": 1170.74, "duration": 3.613 }, { "text": "Then I'll provide in parentheses\nthe names of the columns.", "start": 1174.353, "duration": 2.417 }, { "text": "And on the slide a moment ago I\nshowed this on multiple lines.", "start": 1176.77, "duration": 2.618 }, { "text": "That was just for visuals sake.", "start": 1179.388, "duration": 1.292 }, { "text": "These commands can be entirely on\none line if we would like it to.", "start": 1180.68, "duration": 2.98 }, { "text": "So I can say origin, destination,\nand duration and then provide values.", "start": 1183.66, "duration": 4.87 }, { "text": "I can say let's add New\nYork to London, and I'd", "start": 1188.53, "duration": 4.01 }, { "text": "like for this flight to be\nduration of 415 minutes.", "start": 1192.54, "duration": 4.03 }, { "text": "So I type in the command, press Return.", "start": 1196.57, "duration": 2.03 }, { "text": "That adds a new row to my flights table.", "start": 1198.6, "duration": 3.03 }, { "text": "I can verify this by running\nSELECT * from flights.", "start": 1201.63, "duration": 4.62 }, { "text": "And I see that now I have\nthis table whose id is one--", "start": 1206.25, "duration": 3.09 }, { "text": "that's the id column\nfrom New York to London--", "start": 1209.34, "duration": 3.51 }, { "text": "with a duration of 415 minutes.", "start": 1212.85, "duration": 2.46 }, { "text": "I could do this again and again.", "start": 1215.31, "duration": 1.46 }, { "text": "I, in advance, prepared\na couple of INSERT", "start": 1216.77, "duration": 1.75 }, { "text": "queries just to give us\na little bit more data.", "start": 1218.52, "duration": 2.34 }, { "text": "Each of this is just a\ndifferent INSERT query", "start": 1220.86, "duration": 2.74 }, { "text": "that's going to add one new\nflight into our database.", "start": 1223.6, "duration": 2.94 }, { "text": "So I'll go ahead and copy those\nand then into the database", "start": 1226.54, "duration": 2.72 }, { "text": "I'll go ahead and paste in\nall these INSERT commands", "start": 1229.26, "duration": 2.31 }, { "text": "to insert a whole bunch of\nflights into this database.", "start": 1231.57, "duration": 3.36 }, { "text": "Now if I run SELECT * from flights\nwhat I get is all of the flights.", "start": 1234.93, "duration": 5.49 }, { "text": "This is not formatted\nparticularly nicely.", "start": 1240.42, "duration": 1.94 }, { "text": "SQLite has some additional tricks if\nyou'd like to format it a little nicer.", "start": 1242.36, "duration": 3.23 }, { "text": "So I can say, go ahead and\nput this into columns mode,", "start": 1245.59, "duration": 3.32 }, { "text": "and it gives me some headers.", "start": 1248.91, "duration": 2.28 }, { "text": "So .headers yes.", "start": 1251.19, "duration": 3.03 }, { "text": "And now if I SELECT *\nfrom flights, the data", "start": 1254.22, "duration": 2.74 }, { "text": "is organized a little more nicely\nwhere things are really into columns", "start": 1256.96, "duration": 2.93 }, { "text": "and there are headers\nfor each of the rows.", "start": 1259.89, "duration": 1.792 }, { "text": "Where I can see that I kind have a\ntext-based representation of what", "start": 1261.682, "duration": 3.748 }, { "text": "I was displaying\ngraphically a moment ago.", "start": 1265.43, "duration": 1.75 }, { "text": "That inside of my flights table I have\nfour columns-- an id column, origin,", "start": 1267.18, "duration": 4.24 }, { "text": "destination, and duration.", "start": 1271.42, "duration": 1.67 }, { "text": "Each flight is just one of the\nrows that's inside of this table.", "start": 1273.09, "duration": 5.22 }, { "text": "Now I can begin to run\nadditional SELECT queries.", "start": 1278.31, "duration": 2.62 }, { "text": "If I want to do something like SELECT *\nfrom flights where origin= \"New York,\"", "start": 1280.93, "duration": 7.19 }, { "text": "well then instead of getting\nback all of the flights,", "start": 1288.12, "duration": 2.61 }, { "text": "I'm only going to get back the\nflights where the origin is New York.", "start": 1290.73, "duration": 2.97 }, { "text": "So I can very quickly\nfilter down on this data,", "start": 1293.7, "duration": 2.64 }, { "text": "only getting the information that I\nactually care about taking a look at.", "start": 1296.34, "duration": 3.45 }, { "text": "There are other ways of\ninteracting with SQLite as well,", "start": 1299.79, "duration": 2.43 }, { "text": "but this SQLite prompt\nis a very direct way", "start": 1302.22, "duration": 2.22 }, { "text": "of just being able to\nwrite a query very quickly", "start": 1304.44, "duration": 2.19 }, { "text": "and see the results of that query.", "start": 1306.63, "duration": 2.835 }, { "text": "So it turns out, there are other\ntypes of queries we can run as well.", "start": 1309.465, "duration": 2.875 }, { "text": "So we'll take a look at some others.", "start": 1312.34, "duration": 1.5 }, { "text": "We don't just need to say where\nsomething equals something else.", "start": 1313.84, "duration": 3.29 }, { "text": "Other types of Boolean\nexpressions are allowed as well.", "start": 1317.13, "duration": 2.76 }, { "text": "So I could say something\nlike SELECT * from flights", "start": 1319.89, "duration": 2.91 }, { "text": "where duration is greater than 500.", "start": 1322.8, "duration": 2.41 }, { "text": "Where the idea here is let's\nlook at the duration column.", "start": 1325.21, "duration": 2.81 }, { "text": "It's an integer, so we can do\narithmetic expressions on these values.", "start": 1328.02, "duration": 4.11 }, { "text": "I can take a value and see if\nit's greater than 500 or not.", "start": 1332.13, "duration": 3.5 }, { "text": "When I do that, what I get back\nis all of the rows that have", "start": 1335.63, "duration": 2.62 }, { "text": "a duration that's greater than 500.", "start": 1338.25, "duration": 2.34 }, { "text": "I can begin to query not just\non whether one value equals", "start": 1340.59, "duration": 2.97 }, { "text": "another value, but also other\ntypes of comparisons as well.", "start": 1343.56, "duration": 3.96 }, { "text": "Much as in Python, as when we had\nBoolean expressions in Python,", "start": 1347.52, "duration": 3.12 }, { "text": "I could join multiple\nBoolean expressions together", "start": 1350.64, "duration": 2.67 }, { "text": "using words like AND and OR.", "start": 1353.31, "duration": 2.4 }, { "text": "SQL has the same type of thing\nin its own query syntax, too.", "start": 1355.71, "duration": 3.6 }, { "text": "I could say something like SELECT * from\nflights where duration is greater than", "start": 1359.31, "duration": 3.99 }, { "text": "500 AND destination= \"Paris.\"", "start": 1363.3, "duration": 3.06 }, { "text": "As you might logically\nintuit this means,", "start": 1366.36, "duration": 2.1 }, { "text": "this means get me all the\nflights that are going to Paris", "start": 1368.46, "duration": 2.97 }, { "text": "and that take longer than 500 minutes.", "start": 1371.43, "duration": 2.16 }, { "text": "It turns out there's only one\nsuch row inside this table that", "start": 1373.59, "duration": 3.27 }, { "text": "satisfies both of those constraints.", "start": 1376.86, "duration": 2.97 }, { "text": "Of course, instead of\nusing AND I could've also", "start": 1379.83, "duration": 2.13 }, { "text": "used OR to get a different query where\nnow I'm looking for all of the flights", "start": 1381.96, "duration": 6.12 }, { "text": "longer than 500 minutes or\nthe destination is Paris.", "start": 1388.08, "duration": 3.81 }, { "text": "So as long as either of those conditions\nare met, I'm going to get results back.", "start": 1391.89, "duration": 3.93 }, { "text": "So what I get back are some of the\nrows where the destination is Paris,", "start": 1395.82, "duration": 3.36 }, { "text": "some rows where the\ndestination isn't Paris,", "start": 1399.18, "duration": 2.01 }, { "text": "but the duration is\nmore than 500 minutes,", "start": 1401.19, "duration": 2.74 }, { "text": "and of course any row that\nsatisfies both of these constraints.", "start": 1403.93, "duration": 3.23 }, { "text": "In particular, flight id number 2.", "start": 1407.16, "duration": 2.7 }, { "text": "That gets resulted as well when I run\nthis particular query on this table.", "start": 1409.86, "duration": 6.028 }, { "text": "There are other types of queries\nI can perform in addition to that.", "start": 1415.888, "duration": 2.792 }, { "text": "So I can say something\nlike SELECT * from flights", "start": 1418.68, "duration": 2.37 }, { "text": "where origin is in-- and then\nin parentheses-- in \"New York\"", "start": 1421.05, "duration": 2.97 }, { "text": "comma \"Lima\" to say, check if the origin\nis in this sequence of possible values.", "start": 1424.02, "duration": 4.23 }, { "text": "The sequence of New York and Lima, as\nlong as it is one of those two, then", "start": 1428.25, "duration": 4.05 }, { "text": "I want for the results to come back.", "start": 1432.3, "duration": 1.74 }, { "text": "So then you'll get back rows that\nhave an origin of New York, rows", "start": 1434.04, "duration": 3.36 }, { "text": "that have an origin of Lima, as well.", "start": 1437.4, "duration": 1.68 }, { "text": "So you can begin to construct\nmore sophisticated queries that", "start": 1439.08, "duration": 3.33 }, { "text": "are able to check for whether an\norigin is in a list of possible values", "start": 1442.41, "duration": 3.36 }, { "text": "or even if it matches\na particular pattern.", "start": 1445.77, "duration": 2.97 }, { "text": "That maybe you know that one of\nthe columns looks a certain way", "start": 1448.74, "duration": 2.7 }, { "text": "or is formatted according\nto a particular structure,", "start": 1451.44, "duration": 2.167 }, { "text": "but you don't know\nexactly what the value is.", "start": 1453.607, "duration": 2.943 }, { "text": "I could run a query like SELECT\n* from flights where origin--", "start": 1456.55, "duration": 3.98 }, { "text": "not equals-- but where origin is like--", "start": 1460.53, "duration": 3.36 }, { "text": "and then in quotation marks, I have\na bit of a strange expression \"%a%\"", "start": 1463.89, "duration": 5.49 }, { "text": "This percent stands for a wild card.", "start": 1469.38, "duration": 3.07 }, { "text": "And this time the wild card is looking\nat the values of a particular column.", "start": 1472.45, "duration": 3.48 }, { "text": "In this case, the value of the\norigin column, meaning the percent", "start": 1475.93, "duration": 3.2 }, { "text": "stands for zero or more characters,\nno matter what those characters are.", "start": 1479.13, "duration": 3.81 }, { "text": "We're looking for some number of\ncharacters-- maybe zero maybe more--", "start": 1482.94, "duration": 3.09 }, { "text": "followed by an A, followed by\nsome other number of characters--", "start": 1486.03, "duration": 3.06 }, { "text": "maybe zero maybe more.", "start": 1489.09, "duration": 2.17 }, { "text": "And ultimately what this\nquery is going to do", "start": 1491.26, "duration": 2.39 }, { "text": "is look inside of the flights\ntable and get back to me", "start": 1493.65, "duration": 2.97 }, { "text": "all of the results where\nthere is an A the origin.", "start": 1496.62, "duration": 3.54 }, { "text": "As long as there is an\nA in the origin column,", "start": 1500.16, "duration": 2.13 }, { "text": "doesn't matter if there are characters\nbefore it or characters after it,", "start": 1502.29, "duration": 3.0 }, { "text": "all of the rows that come back\nare going to be the rows that", "start": 1505.29, "duration": 2.82 }, { "text": "just so happened to have an A in it.", "start": 1508.11, "duration": 2.88 }, { "text": "So lots of ways we can\nbegin to run SELECT queries.", "start": 1510.99, "duration": 2.37 }, { "text": "And there are even additional functions\nyou can add to your select queries", "start": 1513.36, "duration": 3.083 }, { "text": "as well.", "start": 1516.443, "duration": 0.757 }, { "text": "If instead of just selecting\nthe values of columns,", "start": 1517.2, "duration": 2.46 }, { "text": "I want to compute the average duration\nof a particular category of flights,", "start": 1519.66, "duration": 3.828 }, { "text": "or I want to count how many flights\nthat are coming out of New York, or I", "start": 1523.488, "duration": 3.042 }, { "text": "want to find the longest\nflight that goes to London,", "start": 1526.53, "duration": 2.19 }, { "text": "or the shortest flight\nthat goes to London,", "start": 1528.72, "duration": 1.86 }, { "text": "or I want to add up all of the\ndurations for a whole bunch of flights.", "start": 1530.58, "duration": 2.917 }, { "text": "Maybe there are flights that\nare connected to each other", "start": 1533.497, "duration": 2.333 }, { "text": "and I want to add up the total\ntravel time that might take.", "start": 1535.83, "duration": 2.61 }, { "text": "SQL has a number of\nbuilt-in functions that", "start": 1538.44, "duration": 2.31 }, { "text": "allow us to perform these\nsorts of calculations, as well.", "start": 1540.75, "duration": 3.663 }, { "text": "So a couple of different SQL\ncommands we've now taken a look at.", "start": 1544.413, "duration": 2.667 }, { "text": "We've taken a look at Create Table\nthat allows us to create a new table.", "start": 1547.08, "duration": 3.87 }, { "text": "INSERT that lets us add data to a table.", "start": 1550.95, "duration": 2.82 }, { "text": "SELECT, which lets us take data\ninside of the table and retrieve it.", "start": 1553.77, "duration": 3.727 }, { "text": "But, of course, often when\nwe're dealing with databases,", "start": 1557.497, "duration": 2.333 }, { "text": "the data doesn't just get added.", "start": 1559.83, "duration": 1.44 }, { "text": "The data changes in some way.", "start": 1561.27, "duration": 1.9 }, { "text": "So we also need some way of being\nable to update data after it's already", "start": 1563.17, "duration": 3.53 }, { "text": "inside of our database.", "start": 1566.7, "duration": 2.14 }, { "text": "There's another query for that,\nwhich is an Update command that we", "start": 1568.84, "duration": 2.9 }, { "text": "can execute on our database.", "start": 1571.74, "duration": 1.92 }, { "text": "And that command looks\nsomething like this.", "start": 1573.66, "duration": 1.86 }, { "text": "Again displayed on multiple lines, but\nyou could put this entirely just on one", "start": 1575.52, "duration": 3.53 }, { "text": "line.", "start": 1579.05, "duration": 0.5 }, { "text": "SQL doesn't really care\nif you add line breaks.", "start": 1579.55, "duration": 2.503 }, { "text": "It just knows that when it sees a\nsemicolon at the end of the command,", "start": 1582.053, "duration": 2.917 }, { "text": "that is the end of this\nparticular command.", "start": 1584.97, "duration": 3.09 }, { "text": "But here what I've said\nis used the Update keyword", "start": 1588.06, "duration": 2.85 }, { "text": "to say I would like to update a table.", "start": 1590.91, "duration": 2.243 }, { "text": "What table would I like to update?", "start": 1593.153, "duration": 1.417 }, { "text": "I'd like to update the flights table.", "start": 1594.57, "duration": 2.34 }, { "text": "What would I like to do?", "start": 1596.91, "duration": 1.47 }, { "text": "Well I'd like to set the\nduration equal to 430.", "start": 1598.38, "duration": 4.29 }, { "text": "So whatever the value of\nduration happens to be right now,", "start": 1602.67, "duration": 2.7 }, { "text": "change it to 430.", "start": 1605.37, "duration": 2.04 }, { "text": "But of course I don't want to change\nit to 430 for every single flight.", "start": 1607.41, "duration": 3.75 }, { "text": "Just as in a SELECT clause, I could say\nWHERE something = something or WHERE", "start": 1611.16, "duration": 3.73 }, { "text": "and then some other\nclause to specify where", "start": 1614.89, "duration": 2.57 }, { "text": "I want the rows to be selected from.", "start": 1617.46, "duration": 2.14 }, { "text": "Likewise, to an Update I can\nsay set duration equal to 430", "start": 1619.6, "duration": 4.22 }, { "text": "WHERE a particular condition is true.", "start": 1623.82, "duration": 2.29 }, { "text": "So here I'm going to look\nthrough the flights table,", "start": 1626.11, "duration": 2.45 }, { "text": "find myself all of the flights\nwhere the origin is New York", "start": 1628.56, "duration": 3.6 }, { "text": "and the destination is London.", "start": 1632.16, "duration": 2.34 }, { "text": "For those rows I'm going to update\nthe value of the duration column", "start": 1634.5, "duration": 3.51 }, { "text": "setting the duration\ncolumn equal to 430.", "start": 1638.01, "duration": 2.91 }, { "text": "So using that syntax I can\ntake data and update it,", "start": 1640.92, "duration": 2.61 }, { "text": "changing one value to another value\nby pinpointing which row or rows I", "start": 1643.53, "duration": 4.32 }, { "text": "would like to change.", "start": 1647.85, "duration": 0.99 }, { "text": "If I only want to change one row, I\nmight do Update flights SET duration=", "start": 1648.84, "duration": 4.77 }, { "text": "something WHERE id= a particular id.", "start": 1653.61, "duration": 3.33 }, { "text": "To be able to pinpoint one\nid and then take that row", "start": 1656.94, "duration": 3.36 }, { "text": "and make some modification to it.", "start": 1660.3, "duration": 2.46 }, { "text": "In addition to inserting data,\nselecting data, and updating data,", "start": 1662.76, "duration": 3.31 }, { "text": "the last main command we'll\nconcern ourselves with", "start": 1666.07, "duration": 2.24 }, { "text": "is the ability to delete data.", "start": 1668.31, "duration": 1.538 }, { "text": "The ability to take a row and\nsay, I'd like to get rid of it.", "start": 1669.848, "duration": 2.542 }, { "text": "Or take multiple rows\nand get rid of them.", "start": 1672.39, "duration": 2.31 }, { "text": "And so a command like delete from\nflights, where destination= \"Tokyo\"", "start": 1674.7, "duration": 4.41 }, { "text": "as you might imagine deletes from the\nflights table all of the rows that", "start": 1679.11, "duration": 3.54 }, { "text": "satisfy this condition where the\ndestination is equal to Tokyo.", "start": 1682.65, "duration": 4.482 }, { "text": "So a number of different operations\nnow that we have the ability to do.", "start": 1687.132, "duration": 2.958 }, { "text": "The ability to delete from a particular\ntable where a condition is true,", "start": 1690.09, "duration": 3.6 }, { "text": "the ability to update a table based\non particular conditions, the ability", "start": 1693.69, "duration": 3.45 }, { "text": "to select data from a table, and\ninsert data into a table as well.", "start": 1697.14, "duration": 4.62 }, { "text": "There are a couple other clauses that\ncan be used to add SQL queries as well.", "start": 1701.76, "duration": 3.81 }, { "text": "Just to add additional functionality.", "start": 1705.57, "duration": 2.28 }, { "text": "If I don't want all of the rows to\ncome back from a particular SQL query,", "start": 1707.85, "duration": 3.6 }, { "text": "I can limit the results that come back.", "start": 1711.45, "duration": 2.04 }, { "text": "So normally SELECT *\nfrom flights would get me", "start": 1713.49, "duration": 2.67 }, { "text": "all of the flights inside of the table.", "start": 1716.16, "duration": 2.16 }, { "text": "But I could say SELECT * from\nflights, LIMIT 5 to just say,", "start": 1718.32, "duration": 3.9 }, { "text": "I only want five results to\ncome back from this table.", "start": 1722.22, "duration": 3.9 }, { "text": "ORDER BY allows me to decide how the\nresults are ordered inside the results", "start": 1726.12, "duration": 4.02 }, { "text": "to come back.", "start": 1730.14, "duration": 0.84 }, { "text": "So I can say SELECT * from flights,\nORDER BY destination, or ORDER BY", "start": 1730.98, "duration": 3.99 }, { "text": "duration to get all of the flights\nin order by how long they are.", "start": 1734.97, "duration": 3.9 }, { "text": "GROUP BY allows me to group a\nwhole bunch of rows together.", "start": 1738.87, "duration": 3.97 }, { "text": "So if I wanted to group all\nof the flights by their origin", "start": 1742.84, "duration": 2.913 }, { "text": "so I can get all of the flights leaving\nNew York and all the flights leaving", "start": 1745.753, "duration": 3.167 }, { "text": "London and so forth, I could\ndo something like SELECT *", "start": 1748.92, "duration": 2.73 }, { "text": "from flights GROUP BY origin to group\nflights by their origin as well.", "start": 1751.65, "duration": 4.83 }, { "text": "HAVING is a constraint\nI can place on GROUP BY", "start": 1756.48, "duration": 2.73 }, { "text": "to say that I would like to select\nall of the flights grouping them", "start": 1759.21, "duration": 4.62 }, { "text": "by their origin, but they need to\nhave a count of at least three.", "start": 1763.83, "duration": 3.27 }, { "text": "Meaning there needs to be\nat least three flights that", "start": 1767.1, "duration": 2.55 }, { "text": "are leaving from that particular city.", "start": 1769.65, "duration": 1.74 }, { "text": "For all of these\nparticular clauses, these", "start": 1772.23, "duration": 1.83 }, { "text": "are helpful to know if you're\ngoing to be directly writing SQL.", "start": 1774.06, "duration": 2.64 }, { "text": "We won't worry about them\ntoo much here, in particular", "start": 1776.7, "duration": 2.31 }, { "text": "because fairly shortly we're not\ngoing to be writing SQL ourselves.", "start": 1779.01, "duration": 3.09 }, { "text": "We're going to just\nbe writing Python code", "start": 1782.1, "duration": 2.13 }, { "text": "and Django is going to, under the\nhood, be manipulating the database", "start": 1784.23, "duration": 3.27 }, { "text": "and creating the SQL\ncommands that it is going", "start": 1787.5, "duration": 2.43 }, { "text": "to run on the underlying database.", "start": 1789.93, "duration": 2.09 }, { "text": "So we will see how we don't\nactually need to worry", "start": 1792.02, "duration": 2.29 }, { "text": "about writing the specific syntax.", "start": 1794.31, "duration": 1.8 }, { "text": "But Django is going to\nhandle much of that for us.", "start": 1796.11, "duration": 3.97 }, { "text": "So here now we have a\nflights table, a table", "start": 1800.08, "duration": 2.42 }, { "text": "that keeps track of all of\nthe flights that we have,", "start": 1802.5, "duration": 2.46 }, { "text": "organizing them by their id and\ntheir origin and their destination", "start": 1804.96, "duration": 3.54 }, { "text": "and their duration.", "start": 1808.5, "duration": 1.2 }, { "text": "But oftentimes when we're dealing with\ndata, especially in a larger database,", "start": 1809.7, "duration": 3.61 }, { "text": "we don't just have one table of data.", "start": 1813.31, "duration": 2.15 }, { "text": "We have multiple tables of data.", "start": 1815.46, "duration": 1.87 }, { "text": "And those multiple tables might\nrelate to each other in some way.", "start": 1817.33, "duration": 4.198 }, { "text": "Let's take a look at an example\nof how that might come about.", "start": 1821.528, "duration": 2.542 }, { "text": "We're going to introduce a concept\nthat will call foreign keys,", "start": 1824.07, "duration": 2.7 }, { "text": "and we'll see what that\nmeans in just a moment.", "start": 1826.77, "duration": 2.77 }, { "text": "So here again is our flights table.", "start": 1829.54, "duration": 2.15 }, { "text": "The flights table has four columns-- an\nid, origin, destination, and duration.", "start": 1831.69, "duration": 5.58 }, { "text": "But of course in New York,\nthere are multiple airports.", "start": 1837.27, "duration": 3.945 }, { "text": "And so it might not make\nsense for me to just", "start": 1841.215, "duration": 1.875 }, { "text": "label each origin or each destination\njust by the name of the city.", "start": 1843.09, "duration": 3.75 }, { "text": "Maybe I also want to give\nthe three letter airport", "start": 1846.84, "duration": 2.7 }, { "text": "code that corresponds to the airport\nto which I'm referring in this case.", "start": 1849.54, "duration": 4.53 }, { "text": "So how would I encode into this\ntable, not only the origin, but also", "start": 1854.07, "duration": 3.66 }, { "text": "that city's airport code?", "start": 1857.73, "duration": 1.44 }, { "text": "And not only for the destination\nthe name of the city,", "start": 1859.17, "duration": 2.26 }, { "text": "but also the airport code\nfor that airport as well?", "start": 1861.43, "duration": 3.02 }, { "text": "Well I could just add more columns.", "start": 1864.45, "duration": 2.0 }, { "text": "I could say something\nlike, all right, now", "start": 1866.45, "duration": 1.75 }, { "text": "we have this table that has an id, an\norigin, an origin code, a destination,", "start": 1868.2, "duration": 6.33 }, { "text": "a destination code, and a duration.", "start": 1874.53, "duration": 2.88 }, { "text": "But here now, the table is\nstarting to get fairly wide.", "start": 1877.41, "duration": 3.64 }, { "text": "There are a lot of columns\nhere and in particular, there", "start": 1881.05, "duration": 2.68 }, { "text": "is some duplicate data.", "start": 1883.73, "duration": 3.37 }, { "text": "Paris is associated with\nthis particular three letter", "start": 1887.1, "duration": 2.91 }, { "text": "code, and the same thing for New\nYork and other airports as well.", "start": 1890.01, "duration": 3.67 }, { "text": "There is some messiness in\nthe structure of this data.", "start": 1893.68, "duration": 3.65 }, { "text": "Often what we'll want to\ndo as we begin to deal", "start": 1897.33, "duration": 2.01 }, { "text": "with data in larger and larger\nsets with more and more columns", "start": 1899.34, "duration": 3.18 }, { "text": "is we'll want to normalize this data.", "start": 1902.52, "duration": 1.89 }, { "text": "Separating things out into\nmultiple different tables that", "start": 1904.41, "duration": 3.3 }, { "text": "just reference one another in some way.", "start": 1907.71, "duration": 3.14 }, { "text": "So instead of just having a single\nflights table what we might consider", "start": 1910.85, "duration": 3.28 }, { "text": "doing is saying that flights\nare one type of object", "start": 1914.13, "duration": 3.48 }, { "text": "but another type of object that\nI care about is an airport.", "start": 1917.61, "duration": 3.75 }, { "text": "So I might just have a separate\ntable just for airports", "start": 1921.36, "duration": 2.91 }, { "text": "where this table has three columns.", "start": 1924.27, "duration": 2.21 }, { "text": "A column for the ID of the\nairport, just some unique number", "start": 1926.48, "duration": 2.74 }, { "text": "that can identify a particular airport.", "start": 1929.22, "duration": 2.22 }, { "text": "One column for the three\nletter code for that airport,", "start": 1931.44, "duration": 2.85 }, { "text": "and one letter for the\ncity, or one column", "start": 1934.29, "duration": 2.07 }, { "text": "for the city where that airport is in.", "start": 1936.36, "duration": 2.7 }, { "text": "Now this is a much more\nstraightforward, simpler representation", "start": 1939.06, "duration": 3.15 }, { "text": "of all of the airports.", "start": 1942.21, "duration": 1.71 }, { "text": "The question becomes what\nhappens to my flights table?", "start": 1943.92, "duration": 3.18 }, { "text": "My flights table that here had an id, an\norigin, destination, and duration where", "start": 1947.1, "duration": 5.31 }, { "text": "the type of origin and destination\nwere in this case just text.", "start": 1952.41, "duration": 4.2 }, { "text": "Text-based data representing\nthe name of the city", "start": 1956.61, "duration": 3.09 }, { "text": "from which the flight is departing\nor to which the flight is arriving.", "start": 1959.7, "duration": 4.04 }, { "text": "Well now that I have\nthe separate airports", "start": 1963.74, "duration": 1.75 }, { "text": "table where every row\nin the airports table", "start": 1965.49, "duration": 2.46 }, { "text": "has its own unique id, then\nwhat I can do in this case is,", "start": 1967.95, "duration": 3.49 }, { "text": "instead of storing an origin\nand a destination as text,", "start": 1971.44, "duration": 3.26 }, { "text": "I can store what we'll\ncall a foreign key.", "start": 1974.7, "duration": 2.25 }, { "text": "A reference to a key in another\ntable and rename these columns", "start": 1976.95, "duration": 3.78 }, { "text": "to origin id and destination id.", "start": 1980.73, "duration": 3.39 }, { "text": "That instead of storing text are\ngoing to store a number where", "start": 1984.12, "duration": 4.38 }, { "text": "origin_id 1 means the\norigin of flight one", "start": 1988.5, "duration": 3.36 }, { "text": "is whatever airport\nnumber 1 happens to be.", "start": 1991.86, "duration": 4.34 }, { "text": "I could go to the airports table,\nlook up which airport has an id of 1,", "start": 1996.2, "duration": 4.96 }, { "text": "and that would tell me\nthe origin of this flight.", "start": 2001.16, "duration": 2.23 }, { "text": "And if I went to the airports\ntable and looked up which airport", "start": 2003.39, "duration": 2.69 }, { "text": "had an ID of 4 that would tell me the\ndestination of this flight as well.", "start": 2006.08, "duration": 4.33 }, { "text": "So by combining two different tables.", "start": 2010.41, "duration": 2.57 }, { "text": "One table for representing airports,\nand one table for representing flights.", "start": 2012.98, "duration": 4.17 }, { "text": "I'm able to connect these\ntwo different tables together", "start": 2017.15, "duration": 3.36 }, { "text": "by way of a foreign key.", "start": 2020.51, "duration": 1.2 }, { "text": "Some columns inside of my flights\ntable, namely the origin id column", "start": 2021.71, "duration": 3.99 }, { "text": "and the destination id\ncolumn that together", "start": 2025.7, "duration": 2.52 }, { "text": "allow me to reference information\nstored inside of another table as well.", "start": 2028.22, "duration": 4.62 }, { "text": "As you imagine this sort\nof airlines database", "start": 2032.84, "duration": 2.49 }, { "text": "growing and storing more\ndifferent kinds of data,", "start": 2035.33, "duration": 2.28 }, { "text": "the ability to relate\ntables to each other", "start": 2037.61, "duration": 2.48 }, { "text": "is going to become incredibly powerful.", "start": 2040.09, "duration": 2.097 }, { "text": "So one thing you might\nimagine is that in addition", "start": 2042.187, "duration": 2.083 }, { "text": "to storing airports and storing\nflights, an airline probably", "start": 2044.27, "duration": 3.36 }, { "text": "also needs to store information\nabout its passengers,", "start": 2047.63, "duration": 2.7 }, { "text": "like who on which flight.", "start": 2050.33, "duration": 2.09 }, { "text": "So you could imagine\nconstructing a passenger's table", "start": 2052.42, "duration": 2.98 }, { "text": "that has an id column to uniquely\nidentify every passenger, a first name", "start": 2055.4, "duration": 3.96 }, { "text": "column that stores every\npassenger's first name,", "start": 2059.36, "duration": 2.24 }, { "text": "a last name column for\nstoring their last name,", "start": 2061.6, "duration": 2.11 }, { "text": "and a flight id column for storing what\nflight that passenger happens to be on.", "start": 2063.71, "duration": 5.08 }, { "text": "So in this case, I could say that\nHarry Potter is on flight number one.", "start": 2068.79, "duration": 3.77 }, { "text": "I could look that up\nin the flights table", "start": 2072.56, "duration": 1.889 }, { "text": "to find out inside the\nflights table, here", "start": 2074.449, "duration": 2.311 }, { "text": "is where the flight is leaving\nfrom, where it's going to", "start": 2076.76, "duration": 2.61 }, { "text": "and what its duration happens to be.", "start": 2079.37, "duration": 2.94 }, { "text": "Now as we begin to\ndesign these tables, we", "start": 2082.31, "duration": 2.369 }, { "text": "have to think about what the\nimplications of that design", "start": 2084.679, "duration": 3.091 }, { "text": "happen to be.", "start": 2087.77, "duration": 0.929 }, { "text": "In the case of this\npassenger's table, it", "start": 2088.699, "duration": 1.98 }, { "text": "does seem that there is a limitation on\nthe table design that I have created.", "start": 2090.679, "duration": 4.591 }, { "text": "Namely, if you think about it you'll\nsee the limitation of this table design", "start": 2095.27, "duration": 3.27 }, { "text": "is that any particular row can only\nhave one flight id associated with it.", "start": 2098.54, "duration": 5.94 }, { "text": "That Harry Potter has a\nsingle flight id column", "start": 2104.48, "duration": 3.12 }, { "text": "that can only have one\nvalue stored inside of it.", "start": 2107.6, "duration": 3.18 }, { "text": "This would seem to make\nit impossible to allow", "start": 2110.78, "duration": 2.55 }, { "text": "for us to be able to\nrepresent a person that", "start": 2113.33, "duration": 2.25 }, { "text": "could be on multiple different flights.", "start": 2115.58, "duration": 3.0 }, { "text": "So this starts to get at the idea\nof different types of relationships", "start": 2118.58, "duration": 3.78 }, { "text": "that rows in a table\ncan have to one another.", "start": 2122.36, "duration": 2.97 }, { "text": "One type of relationship\nis a many to one", "start": 2125.33, "duration": 2.67 }, { "text": "relationship, or a one\nto many relationship,", "start": 2128.0, "duration": 2.29 }, { "text": "where I express the\nidea that one flight can", "start": 2130.29, "duration": 2.78 }, { "text": "be associated with many different\npassengers, for instance.", "start": 2133.07, "duration": 3.87 }, { "text": "But we might also want is a\nmany to many relationship,", "start": 2136.94, "duration": 3.7 }, { "text": "where many different passengers can be\nassociated with many different flights.", "start": 2140.64, "duration": 3.27 }, { "text": "A passenger might have\nmore than one flight.", "start": 2143.91, "duration": 2.42 }, { "text": "A flight might have\nmore than one passenger.", "start": 2146.33, "duration": 2.49 }, { "text": "To do that we're going to need\na slightly different structure", "start": 2148.82, "duration": 3.27 }, { "text": "for this particular type of table.", "start": 2152.09, "duration": 2.19 }, { "text": "One way we could approach it\nis by creating a separate table", "start": 2154.28, "duration": 3.42 }, { "text": "for storing people.", "start": 2157.7, "duration": 1.35 }, { "text": "I can have a people table where every\nperson has an id, has a first name,", "start": 2159.05, "duration": 4.17 }, { "text": "and has a last name same as before.", "start": 2163.22, "duration": 1.95 }, { "text": "But I'm no longer storing flight\ninformation inside of the table.", "start": 2165.17, "duration": 3.66 }, { "text": "I've cleaned my setup up.", "start": 2168.83, "duration": 1.38 }, { "text": "I'm only storing people in this\ntable and nothing about their flight", "start": 2170.21, "duration": 3.36 }, { "text": "information.", "start": 2173.57, "duration": 1.31 }, { "text": "I'll have a separate table for\ndealing with passengers on the flight", "start": 2174.88, "duration": 3.31 }, { "text": "and mapping people to their flights.", "start": 2178.19, "duration": 2.443 }, { "text": "We can think about what does\nthat table need to look like?", "start": 2180.633, "duration": 2.417 }, { "text": "Well I need some sort of table that is\ngoing to relate people to what flights", "start": 2183.05, "duration": 4.35 }, { "text": "they happen to be on.", "start": 2187.4, "duration": 1.29 }, { "text": "So odds are we're going to need one\ncolumn that is a foreign key that", "start": 2188.69, "duration": 3.09 }, { "text": "references this people table, and\nwe'll need another column that", "start": 2191.78, "duration": 3.39 }, { "text": "is a foreign key that references\nthe flights table, such", "start": 2195.17, "duration": 2.97 }, { "text": "that I can relate those\ntwo tables together.", "start": 2198.14, "duration": 2.55 }, { "text": "So that table could look like this.", "start": 2200.69, "duration": 2.67 }, { "text": "This now is a simplified passengers\ntable that only has two columns.", "start": 2203.36, "duration": 3.58 }, { "text": "It has a person id column\nand a flight id column.", "start": 2206.94, "duration": 3.56 }, { "text": "The idea of this table now is\nit's known as an association", "start": 2210.5, "duration": 3.06 }, { "text": "table, or a joined table that just\nassociates one value from one table", "start": 2213.56, "duration": 4.23 }, { "text": "with another value from another table.", "start": 2217.79, "duration": 2.34 }, { "text": "This row here, one and one, means\nthe person with an id of one", "start": 2220.13, "duration": 4.26 }, { "text": "is on flight number one.", "start": 2224.39, "duration": 2.22 }, { "text": "I could look up that person\ninside of the people table,", "start": 2226.61, "duration": 3.06 }, { "text": "look up that flight inside\nof the flights table,", "start": 2229.67, "duration": 2.19 }, { "text": "and figure out who the person\nis and what flight they're on.", "start": 2231.86, "duration": 2.92 }, { "text": "Down here, two and four, means\nwhoever the person with an ID of 2", "start": 2234.78, "duration": 3.89 }, { "text": "is on whichever flight\nhappens to have an ID of 4.", "start": 2238.67, "duration": 5.7 }, { "text": "So this now has allowed\nus to be able to represent", "start": 2244.37, "duration": 2.82 }, { "text": "the types of relationships we want.", "start": 2247.19, "duration": 1.74 }, { "text": "We have a table for airports and\na table for flights and any flight", "start": 2248.93, "duration": 4.32 }, { "text": "is going to map to two different\nairports, one destination one origin.", "start": 2253.25, "duration": 4.14 }, { "text": "And any airport might appear\non multiple different flights.", "start": 2257.39, "duration": 2.74 }, { "text": "It's sort of a one to many relationship.", "start": 2260.13, "duration": 2.6 }, { "text": "Then over here, when\nit comes to passengers,", "start": 2262.73, "duration": 2.34 }, { "text": "we've stored people inside\nof a separate table,", "start": 2265.07, "duration": 2.52 }, { "text": "and then had a many to many\nmapping between people and flights", "start": 2267.59, "duration": 3.39 }, { "text": "so that any person could be\non multiple different flights.", "start": 2270.98, "duration": 2.82 }, { "text": "Like here, for example, person number\ntwo is on both flights one and four.", "start": 2273.8, "duration": 4.62 }, { "text": "Likewise, a flight could\nhave multiple people.", "start": 2278.42, "duration": 2.47 }, { "text": "So in this case flight number\nsix has passengers five and six", "start": 2280.89, "duration": 3.83 }, { "text": "that are on that flight as well.", "start": 2284.72, "duration": 1.68 }, { "text": "We've been able to represent\nthose relationships.", "start": 2286.4, "duration": 3.34 }, { "text": "Of course a byproduct of doing this is\nthat now our tables are a little bit", "start": 2289.74, "duration": 4.01 }, { "text": "messier to look at.", "start": 2293.75, "duration": 1.38 }, { "text": "Messy in the sense that it's\nnot immediately obvious to me,", "start": 2295.13, "duration": 2.71 }, { "text": "when I look at this table,\nwhat data I'm looking at.", "start": 2297.84, "duration": 2.48 }, { "text": "I see these numbers, but I don't\nknow what these numbers mean.", "start": 2300.32, "duration": 3.03 }, { "text": "I've separated all these\ntables into different places.", "start": 2303.35, "duration": 2.825 }, { "text": "Now it's a little harder for me to\nfigure out who is on which flight.", "start": 2306.175, "duration": 2.875 }, { "text": "I have to look at this data, look\nup people in the people table,", "start": 2309.05, "duration": 2.91 }, { "text": "look up flights in the\nflights table, and somehow", "start": 2311.96, "duration": 2.22 }, { "text": "associate all of that information\nback together in order", "start": 2314.18, "duration": 3.63 }, { "text": "to draw any sort of conclusion.", "start": 2317.81, "duration": 2.1 }, { "text": "But luckily, SQL makes\nit pretty easy for us", "start": 2319.91, "duration": 2.19 }, { "text": "to be able to take data across\nmultiple different tables", "start": 2322.1, "duration": 3.33 }, { "text": "and join them all back together.", "start": 2325.43, "duration": 2.19 }, { "text": "We can do this using a JOIN\nquery that takes multiple tables", "start": 2327.62, "duration": 4.05 }, { "text": "and joins them together.", "start": 2331.67, "duration": 1.29 }, { "text": "So the syntax for a JOIN query\nmight look something like this.", "start": 2332.96, "duration": 3.58 }, { "text": "And here we'll go back to\njust the two-table setup where", "start": 2336.54, "duration": 2.48 }, { "text": "I have flights and passengers,\nwhere every passenger is", "start": 2339.02, "duration": 3.18 }, { "text": "associated with one flight.", "start": 2342.2, "duration": 1.41 }, { "text": "But you could extend this\nand join multiple tables", "start": 2343.61, "duration": 2.19 }, { "text": "to deal with our more\ncomplex example as well.", "start": 2345.8, "duration": 2.35 }, { "text": "But here, I'd like to select\nevery person's first name", "start": 2348.15, "duration": 3.2 }, { "text": "and their origin and their destination.", "start": 2351.35, "duration": 2.82 }, { "text": "I'm going to select that\nfrom the flights table,", "start": 2354.17, "duration": 2.22 }, { "text": "but I need to join it\nwith the passengers table.", "start": 2356.39, "duration": 2.91 }, { "text": "Then I say ON to indicate\nhow it is these two", "start": 2359.3, "duration": 2.67 }, { "text": "tables are related to one another.", "start": 2361.97, "duration": 2.022 }, { "text": "In this case, I'm saying\nthe way these two tables are", "start": 2363.992, "duration": 2.208 }, { "text": "related to one another is that the\nflight id column of the passengers", "start": 2366.2, "duration": 4.05 }, { "text": "table is associated with the\nid column of the flights table.", "start": 2370.25, "duration": 4.08 }, { "text": "The flights table has an id that\nuniquely identifies every flight,", "start": 2374.33, "duration": 3.63 }, { "text": "and the passengers table\nhas a flight id column", "start": 2377.96, "duration": 2.82 }, { "text": "that uniquely identifies the\nflight that we're referring to", "start": 2380.78, "duration": 3.12 }, { "text": "for this particular passenger.", "start": 2383.9, "duration": 1.74 }, { "text": "And so the result I might get\nis a table that looks like this.", "start": 2385.64, "duration": 2.7 }, { "text": "That gives me everyone's\nfirst name, but also", "start": 2388.34, "duration": 2.39 }, { "text": "their origin and their destination.", "start": 2390.73, "duration": 1.77 }, { "text": "Our origin and destination are going\nto be drawn from that table of flights", "start": 2392.5, "duration": 6.06 }, { "text": "and the first name is going to be\ndrawn from the table of passengers.", "start": 2398.56, "duration": 3.21 }, { "text": "But by using a JOIN query,\nI've been able to take data", "start": 2401.77, "duration": 2.64 }, { "text": "from two separate tables and\njoin them both back together.", "start": 2404.41, "duration": 4.08 }, { "text": "And there are a number of different\ntypes of JOIN queries that I can run.", "start": 2408.49, "duration": 3.36 }, { "text": "What we saw here was just\nthe default JOIN, which", "start": 2411.85, "duration": 2.31 }, { "text": "is otherwise known as an INNER JOIN.", "start": 2414.16, "duration": 2.79 }, { "text": "Effectively, an INNER JOIN\nwill take the two tables,", "start": 2416.95, "duration": 2.88 }, { "text": "it will cross compare them based\non the condition that I specified", "start": 2419.83, "duration": 2.94 }, { "text": "and only return back to me the results\nwhere there's a match on both sides.", "start": 2422.77, "duration": 3.99 }, { "text": "Where we match a passenger's flight\nid with an id in the flights table.", "start": 2426.76, "duration": 4.67 }, { "text": "There are various\ndifferent kinds of outer", "start": 2431.43, "duration": 1.75 }, { "text": "joins if I want to be OK\nwith the idea that maybe", "start": 2433.18, "duration": 2.61 }, { "text": "something on the left\ntable that I'm joining", "start": 2435.79, "duration": 2.01 }, { "text": "doesn't match with\nanything on the right,", "start": 2437.8, "duration": 1.708 }, { "text": "or maybe something on the right\ntable doesn't match with something", "start": 2439.508, "duration": 2.75 }, { "text": "on the left.", "start": 2442.258, "duration": 0.642 }, { "text": "But just know there are other types of\nJOIN queries that I can run as well.", "start": 2442.9, "duration": 4.74 }, { "text": "Other strategies that can be\nhelpful when dealing with SQL tables", "start": 2447.64, "duration": 3.04 }, { "text": "are optimizations we can make\nto make queries more efficient.", "start": 2450.68, "duration": 3.44 }, { "text": "One thing we can do with our\ntables is to create an index", "start": 2454.12, "duration": 3.57 }, { "text": "on a particular table.", "start": 2457.69, "duration": 1.44 }, { "text": "You can think of an index as kind of\nlike the index in the back of a book,", "start": 2459.13, "duration": 3.7 }, { "text": "for example, where if you wanted to be\nable to search for a topic in a text", "start": 2462.83, "duration": 3.43 }, { "text": "book, you could open the textbook and\njust page by page look for every topic", "start": 2466.26, "duration": 3.795 }, { "text": "and just try and find the\ntopic you're looking for.", "start": 2470.055, "duration": 2.125 }, { "text": "But often what you'll be\nable to do if the table has", "start": 2472.18, "duration": 2.49 }, { "text": "an index is go to the index of the\nbook, find the topic you're looking for,", "start": 2474.67, "duration": 4.78 }, { "text": "and that will quickly give\nyou a reference for how", "start": 2479.45, "duration": 2.15 }, { "text": "to get to the right page in question.", "start": 2481.6, "duration": 2.13 }, { "text": "An index on a table operates\nin much the same way.", "start": 2483.73, "duration": 2.64 }, { "text": "It is an additional data\nstructure that can be constructed,", "start": 2486.37, "duration": 3.27 }, { "text": "and it does take time and memory to be\nable to construct this data structure", "start": 2489.64, "duration": 3.18 }, { "text": "and to maintain it anytime you\nupdate the data inside the table.", "start": 2492.82, "duration": 3.73 }, { "text": "But once it exists it makes querying\non a particular column much more", "start": 2496.55, "duration": 4.37 }, { "text": "efficient.", "start": 2500.92, "duration": 0.66 }, { "text": "You can very quickly look\nsomething up in the index", "start": 2501.58, "duration": 2.76 }, { "text": "and find the corresponding\nrows that go along with it.", "start": 2504.34, "duration": 3.07 }, { "text": "So here we can have a command\nlike create an index that we're", "start": 2507.41, "duration": 3.07 }, { "text": "going to call name index\non the passengers table,", "start": 2510.48, "duration": 3.43 }, { "text": "and in particular on\nthe last name column.", "start": 2513.91, "duration": 3.72 }, { "text": "I expect that as I query this\ntable, I'm pretty frequently", "start": 2517.63, "duration": 3.33 }, { "text": "going to be looking up\npassengers by their last name.", "start": 2520.96, "duration": 2.86 }, { "text": "So I would like to create\nan index on that table", "start": 2523.82, "duration": 2.54 }, { "text": "to be able to more efficiently\nsearch for a passenger", "start": 2526.36, "duration": 3.03 }, { "text": "based on their last name as well.", "start": 2529.39, "duration": 2.89 }, { "text": "So that's just a general overview\nof what SQL syntax is all about.", "start": 2532.28, "duration": 3.43 }, { "text": "A syntax that we can use to be able\nto create data tables inside of which", "start": 2535.71, "duration": 4.12 }, { "text": "are storing rows of data where every\nrow consists of some number of columns,", "start": 2539.83, "duration": 4.26 }, { "text": "and every column has a type.", "start": 2544.09, "duration": 1.67 }, { "text": "We have the ability to create\ntables, add data to them,", "start": 2545.76, "duration": 2.89 }, { "text": "update, delete, and get data\nout of those tables as well.", "start": 2548.65, "duration": 3.3 }, { "text": "But as we begin to introduce\nthese new technologies,", "start": 2551.95, "duration": 2.37 }, { "text": "there are always risks\nand potential threats", "start": 2554.32, "duration": 2.19 }, { "text": "that are associated with\nthose technologies as well.", "start": 2556.51, "duration": 2.85 }, { "text": "In SQL, the key one to be aware of is\nwhat's known as a SQL injection attack.", "start": 2559.36, "duration": 5.01 }, { "text": "A security vulnerability\nthat can happen if you're not", "start": 2564.37, "duration": 2.43 }, { "text": "careful about how it is you\nactually execute your SQL commands.", "start": 2566.8, "duration": 4.078 }, { "text": "So where might this come about?", "start": 2570.878, "duration": 1.292 }, { "text": "You might imagine for instance, that\nif a database has some number of users,", "start": 2572.17, "duration": 3.85 }, { "text": "you might be storing those\nusers inside of a database.", "start": 2576.02, "duration": 3.47 }, { "text": "For instance, in a users table\nwhere there's a user name", "start": 2579.49, "duration": 2.64 }, { "text": "column and a password column.", "start": 2582.13, "duration": 1.7 }, { "text": "Though, in practice\nyou probably wouldn't", "start": 2583.83, "duration": 1.12 }, { "text": "want to store passwords\nand clear text, let's", "start": 2584.95, "duration": 1.92 }, { "text": "imagine here for example that you are\nstoring usernames and passwords inside", "start": 2586.87, "duration": 3.87 }, { "text": "of a table.", "start": 2590.74, "duration": 1.35 }, { "text": "We have a log in form\nfor a website that looks", "start": 2592.09, "duration": 2.16 }, { "text": "like this, where you get to type\nin your username and your password.", "start": 2594.25, "duration": 3.145 }, { "text": "So if someone types in\ntheir username and password,", "start": 2597.395, "duration": 2.125 }, { "text": "what might happen is that the web\napplication might look SELECT * FROM", "start": 2599.52, "duration": 4.03 }, { "text": "users, where username= this\nparticular user name highlighted here.", "start": 2603.55, "duration": 4.53 }, { "text": "We're just going to substitute\nthe user name right there.", "start": 2608.08, "duration": 2.49 }, { "text": "And password= and we'll substitute\nthe password over there.", "start": 2610.57, "duration": 3.872 }, { "text": "So if someone tries\nto log into our site,", "start": 2614.442, "duration": 1.708 }, { "text": "like Harry logs in with a password\nof 12345, what we might do", "start": 2616.15, "duration": 4.29 }, { "text": "is run this SELECT query.", "start": 2620.44, "duration": 1.65 }, { "text": "Say SELECT * FROM users\nwhere username is \"Harry\"", "start": 2622.09, "duration": 3.45 }, { "text": "and where the password is 12345.", "start": 2625.54, "duration": 3.12 }, { "text": "Our logic might be if\nwe get results back,", "start": 2628.66, "duration": 2.31 }, { "text": "then that means there is a user whose\nusername is Harry and password is 12345", "start": 2630.97, "duration": 4.92 }, { "text": "and we can go ahead\nand sign that user in.", "start": 2635.89, "duration": 3.21 }, { "text": "But imagine now what\nmight happen if instead,", "start": 2639.1, "duration": 3.36 }, { "text": "the user who typed in a\nusername of hacker\"--.", "start": 2642.46, "duration": 5.49 }, { "text": "Seems like a bit of a\nstrange username to type in.", "start": 2647.95, "duration": 2.252 }, { "text": "It doesn't matter what they\nput in their password here now.", "start": 2650.202, "duration": 2.458 }, { "text": "The result might be that what they\nwould plug into the username is where", "start": 2652.66, "duration": 3.45 }, { "text": "username=\"hacker\" and then the - -.", "start": 2656.11, "duration": 3.18 }, { "text": "It turns out --", "start": 2659.29, "duration": 1.23 }, { "text": "in SQL stands for a comment in SQL.", "start": 2660.52, "duration": 2.16 }, { "text": "It just means ignore everything that\ncomes after it in the same way that", "start": 2662.68, "duration": 3.0 }, { "text": "in Python you can use the hashtag\nsymbol to mean the rest of this line is", "start": 2665.68, "duration": 3.66 }, { "text": "a comment, and the compiler--\nwhoever is running the program--", "start": 2669.34, "duration": 2.64 }, { "text": "should just ignore it.", "start": 2671.98, "duration": 1.12 }, { "text": "So everything after the\n- - kind of gets ignored.", "start": 2673.1, "duration": 3.41 }, { "text": "We've effectively been able\nto bypass the password check.", "start": 2676.51, "duration": 2.88 }, { "text": "Someone could bypass a password\ncheck and log into an account", "start": 2679.39, "duration": 3.69 }, { "text": "even if they were unauthorized to do so.", "start": 2683.08, "duration": 2.16 }, { "text": "So here is a vulnerability\nwithin the SQL syntax.", "start": 2685.24, "duration": 3.48 }, { "text": "If we're not careful about\nwhen we're running SQL syntax,", "start": 2688.72, "duration": 2.77 }, { "text": "we could be running\nuntrusted SQL commands", "start": 2691.49, "duration": 2.86 }, { "text": "that some hacker or some adversary has\nbeen able to plug-in to our program.", "start": 2694.35, "duration": 4.55 }, { "text": "So how do we solve this sort of problem?", "start": 2698.9, "duration": 2.03 }, { "text": "One strategy is to\nescape these characters.", "start": 2700.93, "duration": 2.82 }, { "text": "Escaping just meaning\nadd some back slashes", "start": 2703.75, "duration": 2.52 }, { "text": "that just makes sure that SQL knows\nto treat these as a literal quotation", "start": 2706.27, "duration": 3.57 }, { "text": "mark and a literal dash, and not\nspecial SQL syntax of any sort.", "start": 2709.84, "duration": 4.14 }, { "text": "Another strategy is to use an\nabstraction layer on top of SQL", "start": 2713.98, "duration": 3.57 }, { "text": "so that we don't have to\nwrite the SQL queries at all.", "start": 2717.55, "duration": 2.25 }, { "text": "That's in fact what we're about to do\nas we transition to the world of Django", "start": 2719.8, "duration": 3.84 }, { "text": "to take a look at how when we begin\nto use a web framework like Django,", "start": 2723.64, "duration": 3.36 }, { "text": "we now have the ability to not worry\nabout the nuances of the syntax of SQL", "start": 2727.0, "duration": 4.53 }, { "text": "and just deal a little more high\nlevel with what our models are.", "start": 2731.53, "duration": 2.885 }, { "text": "What the types of objects are that\nwe're dealing with and interacting", "start": 2734.415, "duration": 2.875 }, { "text": "with inside of this application.", "start": 2737.29, "duration": 3.03 }, { "text": "One other concern worth noting\nabout with regards to SQL", "start": 2740.32, "duration": 2.94 }, { "text": "is the possibility of race conditions.", "start": 2743.26, "duration": 2.25 }, { "text": "A race condition is\nsomething that might happen", "start": 2745.51, "duration": 2.46 }, { "text": "anytime you have multiple events that\nare happening in parallel threads,", "start": 2747.97, "duration": 3.81 }, { "text": "so to speak.", "start": 2751.78, "duration": 0.54 }, { "text": "That you have one thing happening and\nanother thing happening simultaneously.", "start": 2752.32, "duration": 3.75 }, { "text": "You might imagine that in the\ncase of social media sites", "start": 2756.07, "duration": 3.12 }, { "text": "where you can like a post, like an image\non Instagram or a tweet on Twitter,", "start": 2759.19, "duration": 3.4 }, { "text": "for example.", "start": 2762.59, "duration": 0.99 }, { "text": "What would happen if two people tried\nto like the same post at the same time?", "start": 2763.58, "duration": 5.03 }, { "text": "If we're not careful about how we\nrun those particular SQL queries", "start": 2768.61, "duration": 3.27 }, { "text": "there's a potential for us to be\nable to get race condition problems,", "start": 2771.88, "duration": 4.65 }, { "text": "where we end up trying to query for\nthe number of likes that a post has", "start": 2776.53, "duration": 3.57 }, { "text": "and then another person\ntries to do the same thing", "start": 2780.1, "duration": 2.278 }, { "text": "and there are conflicts\nwhen we try and update it", "start": 2782.378, "duration": 2.042 }, { "text": "where the result might not be\nwhat we would expect it to be.", "start": 2784.42, "duration": 2.93 }, { "text": "There are a number of\nunexpected results that", "start": 2787.35, "duration": 1.96 }, { "text": "can happen when we deal with\nproblems related to race conditions", "start": 2789.31, "duration": 3.3 }, { "text": "where multiple things are\nhappening simultaneously.", "start": 2792.61, "duration": 2.83 }, { "text": "How do we solve those problems?", "start": 2795.44, "duration": 1.49 }, { "text": "Well, one strategy is to sort\nof place a lock on the database.", "start": 2796.93, "duration": 3.3 }, { "text": "To say, while I'm working\non this database nobody else", "start": 2800.23, "duration": 2.88 }, { "text": "can touch this data.", "start": 2803.11, "duration": 1.18 }, { "text": "Let me finish this\ntransaction, so to speak.", "start": 2804.29, "duration": 2.29 }, { "text": "Finish working on this particular\ntransaction and making all the changes", "start": 2806.58, "duration": 3.31 }, { "text": "I need to make to the database.", "start": 2809.89, "duration": 1.47 }, { "text": "Only after I'm done I can release the\nlock and let someone else go ahead", "start": 2811.36, "duration": 4.26 }, { "text": "and modify the database as well.", "start": 2815.62, "duration": 2.1 }, { "text": "So a number of concerns\nto be aware of as we", "start": 2817.72, "duration": 2.37 }, { "text": "begin dealing in this world of SQL\nand trying to work with databases.", "start": 2820.09, "duration": 5.43 }, { "text": "So now that we've taken a\nlook at the syntax of SQL,", "start": 2825.52, "duration": 2.28 }, { "text": "understanding how these tables\nwork, how they're structured,", "start": 2827.8, "duration": 3.03 }, { "text": "and what it is that we\ncan add to those tables,", "start": 2830.83, "duration": 2.23 }, { "text": "let's go ahead and turn our\nattention in particular to Django", "start": 2833.06, "duration": 3.05 }, { "text": "models, which are a way of representing\ndata inside of a Django application.", "start": 2836.11, "duration": 4.098 }, { "text": "Because where Django is really going\nto get powerful in designing our web", "start": 2840.208, "duration": 3.042 }, { "text": "applications is the ability to\nrepresent data in terms of these models.", "start": 2843.25, "duration": 4.468 }, { "text": "So we're going to go ahead and try\nto create a web application that", "start": 2847.718, "duration": 2.792 }, { "text": "is going to represent what an\nairline might want to store inside", "start": 2850.51, "duration": 3.9 }, { "text": "of its own web application.", "start": 2854.41, "duration": 2.7 }, { "text": "All right.", "start": 2857.11, "duration": 0.5 }, { "text": "So the first thing I want to\ndo is create a Django project.", "start": 2857.61, "duration": 3.16 }, { "text": "So I'll go ahead and type\nDjango-admin START PROJECT", "start": 2860.77, "duration": 2.955 }, { "text": "and the name of my project\nwill just be called \"airline.\"", "start": 2863.725, "duration": 2.375 }, { "text": "I'm creating a project for an\nairline's website for example.", "start": 2866.1, "duration": 3.37 }, { "text": "I'll go ahead and go into\nthe airline directory,", "start": 2869.47, "duration": 2.06 }, { "text": "open that up in my code editor.", "start": 2871.53, "duration": 1.78 }, { "text": "Before I actually\nbegin editing any code,", "start": 2873.31, "duration": 2.37 }, { "text": "remember that every Django project needs\nto have one or more apps within it.", "start": 2875.68, "duration": 4.32 }, { "text": "So the first app that I'll\ncreate for this airline", "start": 2880.0, "duration": 2.61 }, { "text": "is an app for keeping track of flights.", "start": 2882.61, "duration": 3.15 }, { "text": "So keeping track of flight\nrelated information,", "start": 2885.76, "duration": 2.7 }, { "text": "like origins and\ndestinations and durations", "start": 2888.46, "duration": 2.31 }, { "text": "and what passengers\nare on those flights.", "start": 2890.77, "duration": 2.91 }, { "text": "When I create a new app,\nfirst thing I'll need to do", "start": 2893.68, "duration": 2.97 }, { "text": "is go into settings.py\ninside of airline,", "start": 2896.65, "duration": 2.88 }, { "text": "and go ahead and add this\napp as an installed app.", "start": 2899.53, "duration": 3.36 }, { "text": "So \"flights\" is now an\napp that I have installed.", "start": 2902.89, "duration": 3.15 }, { "text": "Then what I'll want to do is say, go\nahead and go into urls.py, which is,", "start": 2906.04, "duration": 6.662 }, { "text": "again, that table of\ncontents for all the", "start": 2912.702, "duration": 1.708 }, { "text": "URLs I can get to for this\nparticular web application.", "start": 2914.41, "duration": 2.46 }, { "text": "I'll IMPORT include, because I want\nto do is when someone visits the path", "start": 2916.87, "duration": 4.29 }, { "text": "of flights/ something, I want\nto take them to flights.urls,", "start": 2921.16, "duration": 5.56 }, { "text": "mapping them to the urls.py file\nthat will be inside of my \"flights\"", "start": 2926.72, "duration": 4.82 }, { "text": "application.", "start": 2931.54, "duration": 1.41 }, { "text": "Of course, now I need a urls.py file\ninside of my \"flights\" application.", "start": 2932.95, "duration": 4.01 }, { "text": "So I go into \"flights\" and create\na new file that I'll call urls.py.", "start": 2936.96, "duration": 4.09 }, { "text": "We can do from FROM.django.urls\nIMPORT path FROM .IMPORT views.", "start": 2943.87, "duration": 5.34 }, { "text": "And then my URL patterns are going\nto go inside of this list right here.", "start": 2949.21, "duration": 4.9 }, { "text": "But before I begin\ndealing with actual URLs,", "start": 2954.11, "duration": 2.15 }, { "text": "the first thing I'm going to\nwant to do is create some models.", "start": 2956.26, "duration": 3.28 }, { "text": "Models are going to be a way\nof creating a Python class that", "start": 2959.54, "duration": 2.93 }, { "text": "is going to represent data that I want\nDjango to store inside of a database.", "start": 2962.47, "duration": 4.62 }, { "text": "So when I create a\nmodel, Django is going", "start": 2967.09, "duration": 1.77 }, { "text": "to figure out what SQL syntax it needs\nto use it to A-- create that table", "start": 2968.86, "duration": 4.08 }, { "text": "but then B-- manipulate that table.", "start": 2972.94, "duration": 1.8 }, { "text": "Selecting and updating and inserting\nanytime I make changes to those models.", "start": 2974.74, "duration": 4.71 }, { "text": "So here what I can do is inside\nof every app that gets created--", "start": 2979.45, "duration": 3.54 }, { "text": "in this case called \"flights\"--\nthere is a models.py file.", "start": 2982.99, "duration": 3.35 }, { "text": "We haven't looked at\nthis before, but this", "start": 2986.34, "duration": 1.75 }, { "text": "is going to be the place where\nwe get to define what models", "start": 2988.09, "duration": 2.98 }, { "text": "are going to exist for our application.", "start": 2991.07, "duration": 2.79 }, { "text": "Every model is going\nto be a Python class.", "start": 2993.86, "duration": 2.31 }, { "text": "You can think of this as having one\nmodel for each of the main tables", "start": 2996.17, "duration": 3.39 }, { "text": "we care about storing information about.", "start": 2999.56, "duration": 2.62 }, { "text": "So let me define a new\nclass called \"flight\"", "start": 3002.18, "duration": 2.42 }, { "text": "that is going to inherit from models\ndot model some creating a new class", "start": 3004.6, "duration": 3.48 }, { "text": "called \"flight\" that\nis going to be a model.", "start": 3008.08, "duration": 3.75 }, { "text": "Then I need to provide inside of\nthis class all of the parameters", "start": 3011.83, "duration": 3.69 }, { "text": "that a flight has.", "start": 3015.52, "duration": 0.77 }, { "text": "What properties does a flight have\nthat I might want to keep track of?", "start": 3016.29, "duration": 3.76 }, { "text": "Well, a flight has an origin.", "start": 3020.05, "duration": 2.13 }, { "text": "And the origin is going\nto be a models.charfield.", "start": 3022.18, "duration": 3.51 }, { "text": "This is all documented\non Django's website", "start": 3025.69, "duration": 1.83 }, { "text": "in terms of the various different\ntypes of fields that exist that I", "start": 3027.52, "duration": 3.3 }, { "text": "can include inside of a Django model.", "start": 3030.82, "duration": 2.25 }, { "text": "Where here I'm saying\nhere is a character field", "start": 3033.07, "duration": 2.71 }, { "text": "whose max length is going to be 64.", "start": 3035.78, "duration": 3.34 }, { "text": "I assume that most city names are not\ngoing to go longer than 64 characters.", "start": 3039.12, "duration": 3.34 }, { "text": "That seems like a reasonable maximum\nlength for the origin of the flight,", "start": 3042.46, "duration": 3.72 }, { "text": "for example.", "start": 3046.18, "duration": 1.32 }, { "text": "Every flight will also\nhave a destination,", "start": 3047.5, "duration": 1.98 }, { "text": "which will be a character field\nwhose max length is also 64.", "start": 3049.48, "duration": 4.41 }, { "text": "Every flight will have a duration,\nwhich will just be an integer field.", "start": 3053.89, "duration": 4.68 }, { "text": "So now this is my very\nfirst Django model.", "start": 3058.57, "duration": 3.03 }, { "text": "It is a class called \"flight\" where\nI've defined all of the properties", "start": 3061.6, "duration": 3.87 }, { "text": "that a flight has, and then\nusing Django syntax to find", "start": 3065.47, "duration": 2.85 }, { "text": "what type they should have as well.", "start": 3068.32, "duration": 1.56 }, { "text": "Every flight has an origin, has a\ndestination, and has a duration.", "start": 3069.88, "duration": 5.31 }, { "text": "But of course nothing\nhere is actually modified", "start": 3075.19, "duration": 2.07 }, { "text": "the database the Django is using\nin order to store information", "start": 3077.26, "duration": 3.93 }, { "text": "about my web application.", "start": 3081.19, "duration": 1.2 }, { "text": "And we can see if we, in\nfact go back to airline,", "start": 3082.39, "duration": 2.32 }, { "text": "and I type LS, what you see here is that\nthere isn't yet a database that exists.", "start": 3084.71, "duration": 5.61 }, { "text": "I just have an airline\ndirectory, a flights directory,", "start": 3090.32, "duration": 2.69 }, { "text": "and a manage.py file.", "start": 3093.01, "duration": 2.52 }, { "text": "So what I'd like to do\nis somehow tell Django", "start": 3095.53, "duration": 3.06 }, { "text": "that you should update the database to\ninclude information about the models", "start": 3098.59, "duration": 4.17 }, { "text": "that I have just created.", "start": 3102.76, "duration": 1.5 }, { "text": "This is a process that we refer\nto in Django and more generally as", "start": 3104.26, "duration": 3.15 }, { "text": "migrations.", "start": 3107.41, "duration": 1.24 }, { "text": "I create a migration to\nsay, here are some changes", "start": 3108.65, "duration": 3.05 }, { "text": "that I would like to\napply to the database.", "start": 3111.7, "duration": 2.37 }, { "text": "Then I migrate them to tell\nDjango to take those changes", "start": 3114.07, "duration": 3.6 }, { "text": "and actually apply them to the database.", "start": 3117.67, "duration": 2.18 }, { "text": "So it's a 2-step process.", "start": 3119.85, "duration": 1.33 }, { "text": "One is creating the migration, the\ninstructions for how to actually go", "start": 3121.18, "duration": 3.93 }, { "text": "about manipulating the database.", "start": 3125.11, "duration": 1.5 }, { "text": "And then one to take that migration step\nof saying now take those instructions", "start": 3126.61, "duration": 4.29 }, { "text": "and actually apply them to\nthe underlying database.", "start": 3130.9, "duration": 3.9 }, { "text": "We can make the migrations via command.", "start": 3134.8, "duration": 2.52 }, { "text": "Again we'll use the manage.py script\nthat has a number of different commands", "start": 3137.32, "duration": 3.66 }, { "text": "that allow us to control various\nparts of the application.", "start": 3140.98, "duration": 2.85 }, { "text": "I'll use python manage .py\nand then MAKE migrations.", "start": 3143.83, "duration": 5.46 }, { "text": "Now what we see is we've created a\nmigration inside of 0001_initial.py", "start": 3149.29, "duration": 6.3 }, { "text": "where in this migration it's\ncreated a model called \"flight.\"", "start": 3155.59, "duration": 5.06 }, { "text": "So if I go ahead and look\nat the migrations directory,", "start": 3160.65, "duration": 3.22 }, { "text": "I see this file has been created for me.", "start": 3163.87, "duration": 2.89 }, { "text": "I didn't have to create it myself.", "start": 3166.76, "duration": 1.85 }, { "text": "This file has instructions\nto Django for how", "start": 3168.61, "duration": 3.72 }, { "text": "to manipulate the database to reflect\nthe changes I have made to the model.", "start": 3172.33, "duration": 4.47 }, { "text": "Here is an instruction to Django\nto create a new model called", "start": 3176.8, "duration": 3.75 }, { "text": "\"flight\" that has these\nparticular fields inside of it.", "start": 3180.55, "duration": 3.75 }, { "text": "It's basing this off of the\nchanges that I made to models.py.", "start": 3184.3, "duration": 3.39 }, { "text": "The model that I added is now\nreflected in this migration.", "start": 3187.69, "duration": 4.59 }, { "text": "Now if I want to apply the migration,\nactually apply it to Django's database,", "start": 3192.28, "duration": 4.47 }, { "text": "I can run python manage.py MIGRATE to\ngo ahead and apply these migrations.", "start": 3196.75, "duration": 5.542 }, { "text": "There are a bunch of default\nmigrations that get applied as well,", "start": 3202.292, "duration": 2.708 }, { "text": "but notice that one of the migrations\nthat gets applied is this one here.", "start": 3205.0, "duration": 3.87 }, { "text": "Applying flights.0001_initial to\nsay let's go ahead and apply that", "start": 3208.87, "duration": 5.1 }, { "text": "migration, create that table that\nis going to represent flights.", "start": 3213.97, "duration": 4.53 }, { "text": "If I type LS now, you'll see that\nnow I have a db.sqlite3 file.", "start": 3218.5, "duration": 5.37 }, { "text": "A SQLite database that is\ngoing to contain a table that", "start": 3223.87, "duration": 3.72 }, { "text": "is going to store all of my flights.", "start": 3227.59, "duration": 2.827 }, { "text": "And so how can I actually\nbegin to manipulate this data?", "start": 3230.417, "duration": 2.333 }, { "text": "How can I interact with\nthese sorts of models?", "start": 3232.75, "duration": 3.72 }, { "text": "I could use direct SQL syntax\nby opening up this database file", "start": 3236.47, "duration": 3.51 }, { "text": "and running commands, but Django\nprovides some nice abstraction layers", "start": 3239.98, "duration": 3.51 }, { "text": "on top of it so that I don't actually\nneed to execute those commands myself.", "start": 3243.49, "duration": 3.63 }, { "text": "I can begin to work more generally with\nPython classes and variables and things", "start": 3247.12, "duration": 4.41 }, { "text": "that I'm used to inside\nthe Python language.", "start": 3251.53, "duration": 2.88 }, { "text": "So I can enter Django's shell where\nI can just run Python commands", "start": 3254.41, "duration": 3.757 }, { "text": "by running python manage.py shell.", "start": 3258.167, "duration": 1.417 }, { "text": "What this does is open\nup a shell or a console", "start": 3262.75, "duration": 1.92 }, { "text": "where I can begin to\nwrite Python commands that", "start": 3264.67, "duration": 3.03 }, { "text": "get executed on this web application.", "start": 3267.7, "duration": 2.67 }, { "text": "The first thing I'd like to\ndo is from flights.models", "start": 3270.37, "duration": 3.99 }, { "text": "let me just import flight.", "start": 3274.36, "duration": 2.66 }, { "text": "So \"flights\" is the name of my app.", "start": 3277.02, "duration": 1.48 }, { "text": "\"Models\" is the name of that file.", "start": 3278.5, "duration": 1.74 }, { "text": "I'm importing the flight class from\nthat models file that I've just created.", "start": 3280.24, "duration": 4.53 }, { "text": "Now what I can do is I\ncan create a new flight.", "start": 3284.77, "duration": 3.54 }, { "text": "I can say something like f= a\nflight whose origin is \"New York\"", "start": 3288.31, "duration": 5.88 }, { "text": "and whose destination is \"London\"\nand whose duration= 415 minutes.", "start": 3294.19, "duration": 6.63 }, { "text": "And then I can say f.save to save\nthat new flight that I have created.", "start": 3300.82, "duration": 4.442 }, { "text": "This syntax-- I'll go ahead and make it\na little bit bigger so you can see it", "start": 3305.262, "duration": 3.208 }, { "text": "a little easier--", "start": 3308.47, "duration": 0.99 }, { "text": "is my way of inserting\ndata into this table.", "start": 3309.46, "duration": 3.67 }, { "text": "I don't need to use an\nINSERT query in SQL.", "start": 3313.13, "duration": 2.39 }, { "text": "I just have to write a Python\ncommand, and Django knows that when", "start": 3315.52, "duration": 3.09 }, { "text": "I create a new flight and save it,\nthat it should run an instant command", "start": 3318.61, "duration": 4.14 }, { "text": "on the underlying SQL tables.", "start": 3322.75, "duration": 1.59 }, { "text": "Where here I've created a new\nflight with this particular origin", "start": 3324.34, "duration": 3.57 }, { "text": "and destination and duration and I've\ngone ahead and saved that flight as", "start": 3327.91, "duration": 4.11 }, { "text": "well.", "start": 3332.02, "duration": 1.04 }, { "text": "If I want to query that flight,\nget information about that flight,", "start": 3333.06, "duration": 3.1 }, { "text": "I can say something\nlike, flight.objects.all", "start": 3336.16, "duration": 4.08 }, { "text": "is the equivalent of a Select-All.", "start": 3340.24, "duration": 1.77 }, { "text": "Get me all of the flights that\nexist inside of my database.", "start": 3342.01, "duration": 4.38 }, { "text": "Here I see I get back a query set,\nwhich is just a set of results.", "start": 3346.39, "duration": 4.35 }, { "text": "Here I have one flight that\ncame back, flight object 1.", "start": 3350.74, "duration": 3.33 }, { "text": "So a flight has been\ncreated for me with ID 1.", "start": 3354.07, "duration": 2.76 }, { "text": "Now \"flight object 1\" probably\nnot all that helpful of a name.", "start": 3356.83, "duration": 3.12 }, { "text": "It'd be nicer if this\nmodel had a cleaner", "start": 3359.95, "duration": 2.16 }, { "text": "way of seeing the name of a\nparticular flight, for example.", "start": 3362.11, "duration": 4.23 }, { "text": "And it turns out we can do that.", "start": 3366.34, "duration": 1.8 }, { "text": "Any model-- I'll go back to\nthe code inside of models.py--", "start": 3368.14, "duration": 3.54 }, { "text": "any model can implement\ndouble underscore str", "start": 3371.68, "duration": 3.84 }, { "text": "function, which returns\na string representation", "start": 3375.52, "duration": 4.2 }, { "text": "of that particular object.", "start": 3379.72, "duration": 1.89 }, { "text": "This applies not just to Django models\nbut to Python classes more generally.", "start": 3381.61, "duration": 3.48 }, { "text": "That if this function returns a\nstring representation of the object,", "start": 3385.09, "duration": 3.31 }, { "text": "let's go ahead and return a\nformatted string that is self.id", "start": 3388.4, "duration": 4.7 }, { "text": "I'll say self.origin\nto self.destination.", "start": 3393.1, "duration": 4.32 }, { "text": "So here what I've said is that the\nstring representation of any flight", "start": 3397.42, "duration": 4.11 }, { "text": "is going to be a string that gives its\nID and then says origin to destination.", "start": 3401.53, "duration": 4.5 }, { "text": "Just a nice clean name that is going\nto represent this particular flight.", "start": 3406.03, "duration": 4.77 }, { "text": "So now if I go back to the shell\nby running python manage.py shell,", "start": 3410.8, "duration": 6.35 }, { "text": "I can say from\nflights.models import Flight.", "start": 3417.15, "duration": 3.508 }, { "text": "I can say, all right,\nlet's let a variable called", "start": 3420.658, "duration": 2.042 }, { "text": "flights be equal to flight.objects.all.", "start": 3422.7, "duration": 3.66 }, { "text": "And now flights is going to be this\nflight, flight 1, New York to London.", "start": 3426.36, "duration": 5.4 }, { "text": "It now has a much nicer,\nstring representation", "start": 3431.76, "duration": 2.22 }, { "text": "of the name, which just makes it a\nlittle bit easier to interact with.", "start": 3433.98, "duration": 3.69 }, { "text": "If I wanted to get just that one flight,\nI can say flight equals flights.first.", "start": 3437.67, "duration": 5.34 }, { "text": "Flights is a query set.", "start": 3443.01, "duration": 1.47 }, { "text": "First gets me that first flight.", "start": 3444.48, "duration": 2.44 }, { "text": "And so now, I have this flight\nfrom New York to London.", "start": 3446.92, "duration": 3.5 }, { "text": "And just as in any Python\nobject, I can begin", "start": 3450.42, "duration": 2.13 }, { "text": "to access properties of that object.", "start": 3452.55, "duration": 2.18 }, { "text": "I can say, all right,\nflight, what is your ID?", "start": 3454.73, "duration": 2.26 }, { "text": "Flight, what is your origin?", "start": 3456.99, "duration": 1.77 }, { "text": "Flight, what is your destination?", "start": 3458.76, "duration": 1.44 }, { "text": "Flight, what is your duration?", "start": 3460.2, "duration": 1.63 }, { "text": "And I can access, as values, all\nof the properties of this flight", "start": 3461.83, "duration": 3.54 }, { "text": "that I ultimately care about.", "start": 3465.37, "duration": 1.22 }, { "text": "And if I want to delete the flight, I\ncan say something like flight.delete.", "start": 3466.59, "duration": 5.13 }, { "text": "Now, ultimately though,\nthis is not the model", "start": 3471.72, "duration": 2.79 }, { "text": "that I actually want\nto represent my flight.", "start": 3474.51, "duration": 2.1 }, { "text": "Because here again, I'm using a\ncharacter field, a char field,", "start": 3476.61, "duration": 3.48 }, { "text": "for things like origin and destination.", "start": 3480.09, "duration": 1.92 }, { "text": "When in reality, I'd probably like\nto use something like another table", "start": 3482.01, "duration": 4.38 }, { "text": "for representing airports,\nand then some relationship", "start": 3486.39, "duration": 3.36 }, { "text": "between every flight and an airport.", "start": 3489.75, "duration": 2.34 }, { "text": "So let's go ahead and try\nand implement that idea now,", "start": 3492.09, "duration": 2.46 }, { "text": "that I can go back into\nmodels.py and create a new class.", "start": 3494.55, "duration": 4.29 }, { "text": "I'll create a class called airport.", "start": 3498.84, "duration": 2.4 }, { "text": "That is also a model.", "start": 3501.24, "duration": 1.77 }, { "text": "And I'd like for this\nairport class to have", "start": 3503.01, "duration": 2.52 }, { "text": "a code, which is a character\nfield with a max length of 3,", "start": 3505.53, "duration": 5.43 }, { "text": "for the airports code.", "start": 3510.96, "duration": 1.47 }, { "text": "As well as a city, which\nwould be a character field", "start": 3512.43, "duration": 2.49 }, { "text": "with a max length of 64.", "start": 3514.92, "duration": 2.79 }, { "text": "And let's also give this\nairport a string representation.", "start": 3517.71, "duration": 3.79 }, { "text": "We'll say that the string\nrepresentation of an airport", "start": 3521.5, "duration": 2.39 }, { "text": "will just be the city of the\nairport, and then in parentheses,", "start": 3523.89, "duration": 4.29 }, { "text": "the code of the airport.", "start": 3528.18, "duration": 1.1 }, { "text": "So it will be something like New\nYork, and then in parentheses", "start": 3529.28, "duration": 2.583 }, { "text": "JFK to represent a particular airport.", "start": 3531.863, "duration": 4.147 }, { "text": "And now, our flight model\nneeds to change a little bit.", "start": 3536.01, "duration": 3.27 }, { "text": "No longer will origin and\ndestination be character fields", "start": 3539.28, "duration": 3.27 }, { "text": "that are just storing text.", "start": 3542.55, "duration": 1.53 }, { "text": "But instead, origin is\ngoing to be a foreign key.", "start": 3544.08, "duration": 4.83 }, { "text": "A foreign key that references another\ntable, like the airport table.", "start": 3548.91, "duration": 5.157 }, { "text": "And then, I can provide\nsome additional arguments.", "start": 3554.067, "duration": 2.083 }, { "text": "So this alone would be enough.", "start": 3556.15, "duration": 1.35 }, { "text": "But I can add some additional arguments\nlike on delete equals models.cascade.", "start": 3557.5, "duration": 5.57 }, { "text": "So what does this mean?", "start": 3563.07, "duration": 1.21 }, { "text": "Well, when I have tables that\nare related to each other,", "start": 3564.28, "duration": 3.052 }, { "text": "SQL needs some way of\nknowing what should", "start": 3567.332, "duration": 1.708 }, { "text": "happen if you ever delete something.", "start": 3569.04, "duration": 1.83 }, { "text": "If I have a flight from JFK\nto London, and later in time", "start": 3570.87, "duration": 5.07 }, { "text": "decide to delete JFK\nairport from my database,", "start": 3575.94, "duration": 2.79 }, { "text": "what should happen to that flight?", "start": 3578.73, "duration": 1.65 }, { "text": "What happens to flights when the thing\nthat it is referencing gets deleted?", "start": 3580.38, "duration": 4.44 }, { "text": "What models.cascade means\nis if I were to ever delete", "start": 3584.82, "duration": 2.94 }, { "text": "an airport from the airports\ntable, it's going to also delete", "start": 3587.76, "duration": 3.54 }, { "text": "any of the corresponding flights.", "start": 3591.3, "duration": 1.737 }, { "text": "And there are other on\ndelete parameters you", "start": 3593.037, "duration": 1.833 }, { "text": "can set for saying like, don't\neven let me delete an airport", "start": 3594.87, "duration": 2.57 }, { "text": "if there are flights\nthat are leaving from", "start": 3597.44, "duration": 1.75 }, { "text": "or going to that airport,\nthat's called models.protect.", "start": 3599.19, "duration": 3.06 }, { "text": "But there are other ways of implementing\nsimilar types of constraints.", "start": 3602.25, "duration": 4.41 }, { "text": "And the other argument\nthat I'm going to provide", "start": 3606.66, "duration": 2.04 }, { "text": "is what's called a related name.", "start": 3608.7, "duration": 2.49 }, { "text": "And a related name, as\nwe'll see in just a moment,", "start": 3611.19, "duration": 2.34 }, { "text": "is going to be a way of me accessing\na relationship in the reverse order.", "start": 3613.53, "duration": 4.47 }, { "text": "That from a flight, I can take\na flight and say .origin to get", "start": 3618.0, "duration": 4.17 }, { "text": "the flight's origin in airport.", "start": 3622.17, "duration": 1.648 }, { "text": "But the other question I might want\nto ask is in the reverse order.", "start": 3623.818, "duration": 2.792 }, { "text": "If I have an airport, how do\nI get all of the flights that", "start": 3626.61, "duration": 3.6 }, { "text": "have that airport as an origin?", "start": 3630.21, "duration": 1.83 }, { "text": "And so here, if I give a related\nname to this foreign key,", "start": 3632.04, "duration": 3.24 }, { "text": "Django will automatically\nset up the relationship", "start": 3635.28, "duration": 2.73 }, { "text": "going in that opposite direction.", "start": 3638.01, "duration": 1.99 }, { "text": "And so here, well, if\nwe have an airport,", "start": 3640.0, "duration": 2.39 }, { "text": "and I want to know all of the flights\nthat have that airport as their origin,", "start": 3642.39, "duration": 3.36 }, { "text": "the reasonable name for a related name\nhere is something like departures.", "start": 3645.75, "duration": 4.68 }, { "text": "So if I have an airport,\nI can access all", "start": 3650.43, "duration": 2.61 }, { "text": "of the departures, which gets\nme all of the flights that", "start": 3653.04, "duration": 2.91 }, { "text": "are leaving from that airport.", "start": 3655.95, "duration": 2.4 }, { "text": "And I'll likewise do the same\nthing here for destination.", "start": 3658.35, "duration": 2.61 }, { "text": "Instead of a character field\nit's going to be a foreign key.", "start": 3660.96, "duration": 3.12 }, { "text": "It's going to reference airport.", "start": 3664.08, "duration": 1.92 }, { "text": "When we delete it, we'll\ngo ahead and cascade it.", "start": 3666.0, "duration": 2.31 }, { "text": "And the related name will be arrivals.", "start": 3668.31, "duration": 2.85 }, { "text": "Because if I have an\nairport, I might want", "start": 3671.16, "duration": 1.89 }, { "text": "to access all of the\narrivals, all of the flights", "start": 3673.05, "duration": 2.76 }, { "text": "that correspond to\nflights that are arriving", "start": 3675.81, "duration": 2.91 }, { "text": "at that particular destination.", "start": 3678.72, "duration": 2.675 }, { "text": "And so now, I've done two things.", "start": 3681.395, "duration": 1.375 }, { "text": "I've added a new class\ncalled airport, and I've", "start": 3682.77, "duration": 2.85 }, { "text": "modified my existing flight model.", "start": 3685.62, "duration": 2.76 }, { "text": "So this has changed in my\nPython code, but it hasn't yet", "start": 3688.38, "duration": 2.73 }, { "text": "changed in my database.", "start": 3691.11, "duration": 1.78 }, { "text": "So in order to make the change in the\ndatabase, again, it's a 2-step process.", "start": 3692.89, "duration": 3.78 }, { "text": "Step one, python\nmanage.py, make migrations,", "start": 3696.67, "duration": 3.83 }, { "text": "to say look for any new changes\nthat have been made to models.py,", "start": 3700.5, "duration": 4.87 }, { "text": "and go ahead and create\na migration instruction", "start": 3705.37, "duration": 2.96 }, { "text": "for how to make those\nchanges to the database.", "start": 3708.33, "duration": 2.62 }, { "text": "And here, we see that we've\ncreated a new migration file.", "start": 3710.95, "duration": 4.49 }, { "text": "And this migration is going to\ncreate a model called airport.", "start": 3715.44, "duration": 3.09 }, { "text": "And it's also going to\nalter the destination field", "start": 3718.53, "duration": 2.85 }, { "text": "and alter the origin\nfield on my flight model.", "start": 3721.38, "duration": 2.91 }, { "text": "Because as we know, we've\nchanged destination and origin", "start": 3724.29, "duration": 3.06 }, { "text": "to no longer be character\nfields, but to instead", "start": 3727.35, "duration": 2.43 }, { "text": "be references to a particular airport.", "start": 3729.78, "duration": 2.1 }, { "text": "So that's something that's going\nto need to change in the database.", "start": 3731.88, "duration": 3.15 }, { "text": "And to make that change, I can run\nsomething like python manage.py", "start": 3735.03, "duration": 4.86 }, { "text": "migrate to go ahead and\napply those changes.", "start": 3739.89, "duration": 2.58 }, { "text": "We've now applied this\nmigration that we just created,", "start": 3742.47, "duration": 3.09 }, { "text": "and our database is now up to date.", "start": 3745.56, "duration": 3.39 }, { "text": "So what can we do?", "start": 3748.95, "duration": 1.24 }, { "text": "Well, now I can go ahead\nand go back into the shell.", "start": 3750.19, "duration": 2.96 }, { "text": "And I'll just go ahead and\nimport from flights.models import", "start": 3753.15, "duration": 3.27 }, { "text": "star, import everything.", "start": 3756.42, "duration": 2.23 }, { "text": "And I can now create an airport.", "start": 3758.65, "duration": 1.67 }, { "text": "I can say something like JFK\nequals an airport whose code is JFK", "start": 3760.32, "duration": 5.94 }, { "text": "and whose city is New York, for example.", "start": 3766.26, "duration": 3.18 }, { "text": "And then save that.", "start": 3769.44, "duration": 1.44 }, { "text": "I can create a London one.", "start": 3770.88, "duration": 1.17 }, { "text": "So LHR is an airport whose code\nis LHR and whose city is London.", "start": 3772.05, "duration": 5.6 }, { "text": "And I can save that.", "start": 3777.65, "duration": 1.413 }, { "text": "You could create more.", "start": 3779.063, "duration": 0.917 }, { "text": "I could say CDG equals an airport\nwhose code is CDG and city is Paris.", "start": 3779.98, "duration": 6.29 }, { "text": "And maybe we'll do one more.", "start": 3786.27, "duration": 1.4 }, { "text": "We'll say NRT is the airport whose\ncode is NRT and whose city is Tokyo,", "start": 3787.67, "duration": 5.44 }, { "text": "for example.", "start": 3793.11, "duration": 1.09 }, { "text": "So I've created and saved four airports\nthat get added to my airport table.", "start": 3794.2, "duration": 4.28 }, { "text": "And now, I can add a flight.", "start": 3798.48, "duration": 2.25 }, { "text": "F equals flight whose origin\nequals JFK whose destination equals", "start": 3800.73, "duration": 5.97 }, { "text": "London Heathrow and whose\nduration equals 415 minutes.", "start": 3806.7, "duration": 4.34 }, { "text": "And I'll go ahead and save that as well.", "start": 3811.04, "duration": 4.083 }, { "text": "So I've now created four airports.", "start": 3815.123, "duration": 1.417 }, { "text": "I've created a flight and saved it.", "start": 3816.54, "duration": 2.13 }, { "text": "If I type F just for my\nflight, I see that, all right,", "start": 3818.67, "duration": 3.84 }, { "text": "this is a flight from\nNew York to London.", "start": 3822.51, "duration": 2.73 }, { "text": "But I can also say what is\nf.origin, and to write f.origin,", "start": 3825.24, "duration": 5.49 }, { "text": "that is now an airport object.", "start": 3830.73, "duration": 1.92 }, { "text": "It's JFK, in particular.", "start": 3832.65, "duration": 1.26 }, { "text": "And I can do f.origin.city to get the\ncity of the origin, which is New York.", "start": 3833.91, "duration": 4.58 }, { "text": "f.origin.code to get the code\nof that airport, which is JFK.", "start": 3838.49, "duration": 4.33 }, { "text": "And if I start with an origin,\nsomething like JFK or London Heathrow,", "start": 3842.82, "duration": 3.9 }, { "text": "I can say LHR.arrivals.all to\nget all of the arrivals, all", "start": 3846.72, "duration": 6.48 }, { "text": "of the flights arriving\nin London Heathrow.", "start": 3853.2, "duration": 2.038 }, { "text": "And it looks like there's\njust one of them, which", "start": 3855.238, "duration": 2.042 }, { "text": "is this flight that I've just\ncreated from New York that", "start": 3857.28, "duration": 3.15 }, { "text": "is going to London as well.", "start": 3860.43, "duration": 2.58 }, { "text": "And so this now gives us the\nability to manipulate SQL", "start": 3863.01, "duration": 3.3 }, { "text": "just by using these Python models.", "start": 3866.31, "duration": 1.82 }, { "text": "And I now have Python\nclasses that represent", "start": 3868.13, "duration": 2.59 }, { "text": "all of these various\ndifferent types of data.", "start": 3870.72, "duration": 2.19 }, { "text": "And now, instead of running\nSQL queries like select star", "start": 3872.91, "duration": 2.61 }, { "text": "from flights or from\nairports, I can just", "start": 3875.52, "duration": 2.16 }, { "text": "interact with these classes and\nthese properties on the classes,", "start": 3877.68, "duration": 3.9 }, { "text": "and Django takes care\nof the process for me", "start": 3881.58, "duration": 2.28 }, { "text": "of figuring out what the\nunderlying SQL queries should be,", "start": 3883.86, "duration": 3.03 }, { "text": "executing those queries, and just\ngiving those results back to me.", "start": 3886.89, "duration": 3.78 }, { "text": "And we can begin now to design a\nweb application around this idea.", "start": 3890.67, "duration": 3.9 }, { "text": "That I can go into urls.py, and\nlets add a url pattern that says,", "start": 3894.57, "duration": 3.96 }, { "text": "the default route.", "start": 3898.53, "duration": 1.44 }, { "text": "We'll go ahead and load the index view.", "start": 3899.97, "duration": 2.19 }, { "text": "Give it a name of index.", "start": 3902.16, "duration": 1.2 }, { "text": "Same as similar things\nwe've seen from last time.", "start": 3903.36, "duration": 3.12 }, { "text": "And now, what should we\ndo in the index view?", "start": 3906.48, "duration": 3.06 }, { "text": "Well, the index view, let's go ahead\nand say, what I would like to do", "start": 3909.54, "duration": 4.17 }, { "text": "is just display a list\nof all the flights.", "start": 3913.71, "duration": 2.94 }, { "text": "So I might from.models\nimport flight and airport.", "start": 3916.65, "duration": 5.76 }, { "text": "Or maybe I just need flight.", "start": 3922.41, "duration": 1.297 }, { "text": "I just want a list of all the flights.", "start": 3923.707, "duration": 1.583 }, { "text": "So I'm going to import\nflight from all of my models.", "start": 3925.29, "duration": 2.89 }, { "text": "And now, what I'd like to do is return--", "start": 3928.18, "duration": 2.36 }, { "text": "let's go ahead and render a\ntemplate called flight/index.html,", "start": 3930.54, "duration": 4.8 }, { "text": "and give index.html access\nto a variable called flights.", "start": 3935.34, "duration": 4.62 }, { "text": "And what is that variable\ngoing to be equal to?", "start": 3939.96, "duration": 2.27 }, { "text": "It's going to be equal\nto flight.objects.all", "start": 3942.23, "duration": 3.25 }, { "text": "to get me all of the flights that\nI would like to put right here.", "start": 3945.48, "duration": 4.89 }, { "text": "All right, so what can I do from now?", "start": 3950.37, "duration": 1.66 }, { "text": "Now, what I need to do is actually\ncreate those individual templates.", "start": 3952.03, "duration": 3.38 }, { "text": "So inside of flights, I'll create\na new folder called templates.", "start": 3955.41, "duration": 5.01 }, { "text": "Inside of which I'll create\na new folder called flights.", "start": 3960.42, "duration": 2.88 }, { "text": "Inside of which I'll go ahead\nand create a layout.html,", "start": 3963.3, "duration": 2.73 }, { "text": "much as we've done\nbefore, where that layout", "start": 3966.03, "duration": 2.22 }, { "text": "is going to contain the basic\nstructure of our HTML page.", "start": 3968.25, "duration": 4.56 }, { "text": "So a head section whose title is\nflights, and a body section that", "start": 3972.81, "duration": 6.1 }, { "text": "is going to have a block body,\nand the end of the block.", "start": 3978.91, "duration": 5.31 }, { "text": "Much as before, this is the default\nlayout for this particular page.", "start": 3984.22, "duration": 4.2 }, { "text": "And then, I'll add a new template called\nindex.html that is going to extend", "start": 3988.42, "duration": 5.31 }, { "text": "flight/layout.html.", "start": 3993.73, "duration": 2.67 }, { "text": "And then, inside the\nbody of the page, I'm", "start": 3996.4, "duration": 4.23 }, { "text": "going to display an H1\nthat just says flights.", "start": 4000.63, "duration": 3.21 }, { "text": "And let's now create an\nunordered list where I can now", "start": 4003.84, "duration": 2.67 }, { "text": "loop over for flight in flights.", "start": 4006.51, "duration": 3.51 }, { "text": "endfor to end the loop.", "start": 4010.02, "duration": 1.53 }, { "text": "But inside the loop, let\nme create a list item", "start": 4011.55, "duration": 2.65 }, { "text": "where I just print out a flight--", "start": 4014.2, "duration": 3.44 }, { "text": "maybe I'll print out the\nflight and then flight.id", "start": 4017.64, "duration": 3.9 }, { "text": "to print out flight\n1, flight 2, flight 3.", "start": 4021.54, "duration": 2.4 }, { "text": "And then, I'll print flight.origin\nto flight.destination.", "start": 4023.94, "duration": 6.1 }, { "text": "So what I've done here\nis create a template", "start": 4030.04, "duration": 3.32 }, { "text": "that I'm going to give access to a\nvariable called flights, where flights", "start": 4033.36, "duration": 3.57 }, { "text": "is going to be a variable that\nrepresents all of the flights", "start": 4036.93, "duration": 3.39 }, { "text": "that I queried by running\nflight.objects.all, that is my way", "start": 4040.32, "duration": 3.72 }, { "text": "using Django's API, using the functions\nthat it has given me access to,", "start": 4044.04, "duration": 3.48 }, { "text": "to say, take the flight and get all\nof the flights that are stored inside", "start": 4047.52, "duration": 4.56 }, { "text": "of Django's database.", "start": 4052.08, "duration": 1.44 }, { "text": "Then here in the template, I'm looping\nover each one of those flights.", "start": 4053.52, "duration": 3.3 }, { "text": "For each one, printing\nout a list item where", "start": 4056.82, "duration": 2.91 }, { "text": "I can access properties of that flight.", "start": 4059.73, "duration": 1.87 }, { "text": "Say flight this ID from origin\nto a particular destination.", "start": 4061.6, "duration": 7.07 }, { "text": "So now, I'll go ahead\nand go into my terminal.", "start": 4068.67, "duration": 2.72 }, { "text": "Run python manage.py\nrun server, which again,", "start": 4071.39, "duration": 3.15 }, { "text": "is how we run a Django web application.", "start": 4074.54, "duration": 2.19 }, { "text": "And now, if I go to that\nURL, flash flights this time,", "start": 4076.73, "duration": 3.21 }, { "text": "because that's the URL.", "start": 4079.94, "duration": 1.2 }, { "text": "What I see is exactly\nwhat I'd expect to see.", "start": 4081.14, "duration": 2.19 }, { "text": "An unordered list that just so happens\nto have flight one New York to London", "start": 4083.33, "duration": 5.58 }, { "text": "displayed there.", "start": 4088.91, "duration": 0.8 }, { "text": "It is taking data from my database,\nand now displaying it inside", "start": 4089.71, "duration": 3.49 }, { "text": "of this template.", "start": 4093.2, "duration": 0.95 }, { "text": "And if I were to add new flights, it\nwould also update on this page as well.", "start": 4094.15, "duration": 4.249 }, { "text": "So if I go ahead and go back, go into\nthe shell, python manage.py shell.", "start": 4098.399, "duration": 4.221 }, { "text": "I'll go from flights.models import star.", "start": 4102.62, "duration": 3.569 }, { "text": "Let's go ahead and--", "start": 4106.189, "duration": 2.34 }, { "text": "well, all right, let's add a flight\nfrom Shanghai to Paris, for example.", "start": 4108.529, "duration": 3.04 }, { "text": "Well, how do I get the airports\nfor Shanghai and Paris?", "start": 4111.569, "duration": 3.051 }, { "text": "Well, it turns out that\nif I want to get Shanghai,", "start": 4114.62, "duration": 2.71 }, { "text": "I can say Shanghai\nequals airport.objects.", "start": 4117.33, "duration": 5.029 }, { "text": "and then I can say--", "start": 4122.359, "duration": 1.201 }, { "text": "if I do airport.objects.all, that gets\nme all of the airports, for example.", "start": 4123.56, "duration": 5.259 }, { "text": "Oh, and it seems I don't\nactually have a Shanghai one,", "start": 4128.819, "duration": 2.511 }, { "text": "but I can add one if I wanted to.", "start": 4131.33, "duration": 1.81 }, { "text": "But if I do airport.objects.all,\nthat, again, gives me all of them.", "start": 4133.14, "duration": 3.99 }, { "text": "But if I want to filter my airports\nlist, not get all of the airports", "start": 4137.13, "duration": 3.17 }, { "text": "but just get some of them, I can say\nairport.objects.filter, and I can say,", "start": 4140.3, "duration": 5.82 }, { "text": "get me all the airports where the\ncity is New York, for example.", "start": 4146.12, "duration": 6.409 }, { "text": "And that is going to go ahead\nand give me a query set that only", "start": 4152.529, "duration": 2.921 }, { "text": "contains the results that I care about.", "start": 4155.45, "duration": 2.07 }, { "text": "So again, airport.objects.filter lets\nme constrain the results that come back.", "start": 4157.52, "duration": 4.56 }, { "text": "Not get me all of the\nairports, but only get me", "start": 4162.08, "duration": 2.46 }, { "text": "airports whose city is\nNew York, for example.", "start": 4164.54, "duration": 2.7 }, { "text": "And it is only giving back one, so\nI could say .filter city equals New", "start": 4167.24, "duration": 3.3 }, { "text": "York.first, to say, take that query\nset, and just get me the first and only", "start": 4170.54, "duration": 4.529 }, { "text": "thing in that query set.", "start": 4175.069, "duration": 1.201 }, { "text": "And that gives me airport New York.", "start": 4176.27, "duration": 2.72 }, { "text": "A simplified way of\ndoing the same thing.", "start": 4178.99, "duration": 1.72 }, { "text": "If you know you're only going\nto get one result back, is I", "start": 4180.71, "duration": 2.85 }, { "text": "can say something like\nairport.objects.get,", "start": 4183.56, "duration": 2.1 }, { "text": "which will only get one result if it\nknows that there's only going to be", "start": 4185.66, "duration": 4.35 }, { "text": "one airport with the city of New York.", "start": 4190.01, "duration": 2.25 }, { "text": "That too will return to\nme New York JFK airport.", "start": 4192.26, "duration": 4.38 }, { "text": "But it will throw an error if\never there's more than one,", "start": 4196.64, "duration": 2.38 }, { "text": "or if there's none, for example.", "start": 4199.02, "duration": 1.76 }, { "text": "So we'll go in and save\nthat inside of JFK,", "start": 4200.78, "duration": 2.07 }, { "text": "and we'll go ahead and\ncreate a flight that is going", "start": 4202.85, "duration": 2.64 }, { "text": "from New York to Paris, for example.", "start": 4205.49, "duration": 2.39 }, { "text": "I can do CDG equals airport.objects.get.", "start": 4207.88, "duration": 3.696 }, { "text": "City equals Paris.", "start": 4211.576, "duration": 1.774 }, { "text": "And now, I have this variable CDG,\nwhich represents the airport Paris.", "start": 4213.35, "duration": 4.02 }, { "text": "And if I want to create a new flight\nthat goes from New York to Paris,", "start": 4217.37, "duration": 4.02 }, { "text": "I can say F is going\nto be a flight whose", "start": 4221.39, "duration": 2.19 }, { "text": "origin is JFK whose destination equals\nCDG and whose duration equals 435.", "start": 4223.58, "duration": 6.8 }, { "text": "And I can save that flight as well.", "start": 4230.38, "duration": 2.72 }, { "text": "And so I've added a new flight.", "start": 4233.1, "duration": 1.87 }, { "text": "And so now, if I run the\nserver, python manage.py,", "start": 4234.97, "duration": 3.73 }, { "text": "run server, refresh the page, I\nnow see that I have two flights.", "start": 4238.7, "duration": 4.9 }, { "text": "One flight that's going from New\nYork to London, one flight that's", "start": 4243.6, "duration": 2.75 }, { "text": "going from New York to Paris.", "start": 4246.35, "duration": 1.98 }, { "text": "But of course, it's going to be\npretty annoying if every time I", "start": 4248.33, "duration": 2.76 }, { "text": "want to update the data, adding\nnew data, manipulating the data,", "start": 4251.09, "duration": 3.18 }, { "text": "I need to go into the\nshell in order to run", "start": 4254.27, "duration": 2.55 }, { "text": "direct commands that are\nable to add new flights,", "start": 4256.82, "duration": 2.13 }, { "text": "add new airport, so on and so forth.", "start": 4258.95, "duration": 1.86 }, { "text": "What I'd really like to be\nable to do is just very simply", "start": 4260.81, "duration": 3.0 }, { "text": "to add it via a web interface.", "start": 4263.81, "duration": 1.828 }, { "text": "Via the web, be able\nto say, all right, let", "start": 4265.638, "duration": 1.792 }, { "text": "me add a new flight that goes\nfrom location 1 to location 2.", "start": 4267.43, "duration": 3.82 }, { "text": "And it's possible, using\nthe information we know now,", "start": 4271.25, "duration": 2.64 }, { "text": "to build a web page that does just this.", "start": 4273.89, "duration": 2.73 }, { "text": "But Django is built on this idea that\nit doesn't want you, the programmer,", "start": 4276.62, "duration": 3.84 }, { "text": "to have to repeat work that\nother people have already done.", "start": 4280.46, "duration": 3.0 }, { "text": "And this process of trying to\ndefine models and very quickly be", "start": 4283.46, "duration": 3.63 }, { "text": "able to create and edit\nand manipulate models", "start": 4287.09, "duration": 2.07 }, { "text": "is so common that Django has already\nbuilt for us an entire app that is just", "start": 4289.16, "duration": 5.07 }, { "text": "designed for the\nmanipulation of these models,", "start": 4294.23, "duration": 2.73 }, { "text": "and it's known as the Django admin app.", "start": 4296.96, "duration": 3.06 }, { "text": "And this is an app that\nwe've seen traces of already,", "start": 4300.02, "duration": 2.49 }, { "text": "that if we remember that urls.py\nfile from inside of our application.", "start": 4302.51, "duration": 6.15 }, { "text": "We saw that we added a\npath for our own app,", "start": 4308.66, "duration": 2.94 }, { "text": "but there was already a path\ngiven to us by default, /admin,", "start": 4311.6, "duration": 3.96 }, { "text": "that takes us to the admin app as well.", "start": 4315.56, "duration": 3.37 }, { "text": "And so in order to use\nthe admin app, we need", "start": 4318.93, "duration": 2.54 }, { "text": "to create an administrative account\ninside of our Django web application.", "start": 4321.47, "duration": 4.29 }, { "text": "And the way to do that\nis via the command line.", "start": 4325.76, "duration": 2.34 }, { "text": "I can run python manage.py\ncreate super user.", "start": 4328.1, "duration": 4.775 }, { "text": "It's going to ask me for my name.", "start": 4332.875, "duration": 1.375 }, { "text": "I'll go in and type in my\nuser name, my email address,", "start": 4334.25, "duration": 3.77 }, { "text": "and it's also going\nto ask for a password.", "start": 4338.02, "duration": 1.75 }, { "text": "I can just make up a password\nthat I would like to use.", "start": 4339.77, "duration": 2.79 }, { "text": "Retype it in just to confirm it.", "start": 4342.56, "duration": 1.86 }, { "text": "And now, Django has created a super user\naccount for me in this web application", "start": 4344.42, "duration": 5.61 }, { "text": "so that I, using these\ncredentials, have the ability", "start": 4350.03, "duration": 2.91 }, { "text": "to visit the web interface\nfor the admin app", "start": 4352.94, "duration": 2.46 }, { "text": "and actually manipulate some\nof these underlying models.", "start": 4355.4, "duration": 4.12 }, { "text": "So in order to do this, the first\nthing I need to do is take my models", "start": 4359.52, "duration": 3.83 }, { "text": "and add those models to the admin app.", "start": 4363.35, "duration": 2.43 }, { "text": "So inside of models.py, I have a\nclass called airport and a class", "start": 4365.78, "duration": 3.72 }, { "text": "called flight.", "start": 4369.5, "duration": 1.51 }, { "text": "And if we look at the\nfiles I have, there's", "start": 4371.01, "duration": 1.88 }, { "text": "another file we haven't really looked\nat yet called admin.py inside of my app.", "start": 4372.89, "duration": 4.77 }, { "text": "And inside of admin.py, I'll first from\nmy models import flight and airport.", "start": 4377.66, "duration": 6.24 }, { "text": "And now, I'm going to say,\nadmin.site.register airport.", "start": 4383.9, "duration": 4.41 }, { "text": "And admin.site.register flight.", "start": 4388.31, "duration": 4.88 }, { "text": "And what this is going\nto do is it is going", "start": 4393.19, "duration": 2.11 }, { "text": "to tell Django's admin app that\nI would like to use the admin", "start": 4395.3, "duration": 3.57 }, { "text": "app to be able to manipulate airports\nand to be able to manipulate flights", "start": 4398.87, "duration": 4.59 }, { "text": "as well.", "start": 4403.46, "duration": 1.78 }, { "text": "So let's take a look at this admin\napp and see how it actually works.", "start": 4405.24, "duration": 3.47 }, { "text": "I can run python manage.py run server.", "start": 4408.71, "duration": 4.35 }, { "text": "That will start up the web server.", "start": 4413.06, "duration": 1.92 }, { "text": "I'll now visit this URL.", "start": 4414.98, "duration": 1.23 }, { "text": "Instead of going to\n/flights I'll go /admin.", "start": 4416.21, "duration": 3.39 }, { "text": "And this opens up this\nDjango administration app", "start": 4419.6, "duration": 3.94 }, { "text": "that is not written by me.", "start": 4423.54, "duration": 1.13 }, { "text": "Django has written this, and\nit's asking me to log in.", "start": 4424.67, "duration": 2.82 }, { "text": "I'll go ahead and log in using those\ncredentials I used a moment ago, typing", "start": 4427.49, "duration": 3.36 }, { "text": "in my username and password.", "start": 4430.85, "duration": 1.59 }, { "text": "And what I get here is Django's\nsite administration interface.", "start": 4432.44, "duration": 4.5 }, { "text": "Built for me by Django, where I\ndidn't need to design this at all.", "start": 4436.94, "duration": 3.09 }, { "text": "But importantly, if\nwe noticed down here,", "start": 4440.03, "duration": 2.1 }, { "text": "I now have the ability\nto add and manipulate", "start": 4442.13, "duration": 2.37 }, { "text": "airports and flights via this\nweb interface, this Django", "start": 4444.5, "duration": 3.51 }, { "text": "administrative interface.", "start": 4448.01, "duration": 1.15 }, { "text": "So now, using this\ninterface, I have the ability", "start": 4449.16, "duration": 2.54 }, { "text": "to manipulate the underlying database.", "start": 4451.7, "duration": 1.95 }, { "text": "To manipulate my models to add and\nmodify data that already exists.", "start": 4453.65, "duration": 4.74 }, { "text": "So if I click on airports,\nfor example, I see here,", "start": 4458.39, "duration": 2.78 }, { "text": "here are all of the airports that\nI've already added to my database.", "start": 4461.17, "duration": 2.98 }, { "text": "Tokyo, Paris, London, and New York.", "start": 4464.15, "duration": 2.22 }, { "text": "And I can add a new one.", "start": 4466.37, "duration": 1.08 }, { "text": "I can say, let's go ahead and\nadd PVG which is Shanghai.", "start": 4467.45, "duration": 4.14 }, { "text": "And I can either save it, save and\ncontinue editing, save and add another.", "start": 4471.59, "duration": 3.32 }, { "text": "I'm going to add a couple, so I'll\ngo ahead and save and add another.", "start": 4474.91, "duration": 2.875 }, { "text": "Let's go ahead and add\nIstanbul airport as well.", "start": 4477.785, "duration": 2.925 }, { "text": "Let's add Moscow as an airport two, and\nmaybe one more, we'll add Lima as well.", "start": 4480.71, "duration": 6.84 }, { "text": "And I'll just go ahead and click save.", "start": 4487.55, "duration": 1.62 }, { "text": "And now, I've added a whole bunch of\nairports all via this web interface.", "start": 4489.17, "duration": 3.6 }, { "text": "Django was originally created\nfor news organization that", "start": 4492.77, "duration": 3.02 }, { "text": "very quickly wanted to be\nable to post articles and post", "start": 4495.79, "duration": 2.65 }, { "text": "new posts on their website.", "start": 4498.44, "duration": 1.41 }, { "text": "And it made it very easy\nvia an interface like this", "start": 4499.85, "duration": 2.43 }, { "text": "to very quickly just say,\nhere, add a new article", "start": 4502.28, "duration": 2.52 }, { "text": "and here's the content of the article,\nto be able to display on a page.", "start": 4504.8, "duration": 3.27 }, { "text": "And now, we've been able to very\nquickly add new airports to our website", "start": 4508.07, "duration": 3.81 }, { "text": "as well.", "start": 4511.88, "duration": 1.03 }, { "text": "And so if we want to add flights,\nwell, we can go ahead and go back home.", "start": 4512.91, "duration": 5.09 }, { "text": "Click on flights.", "start": 4518.0, "duration": 1.38 }, { "text": "I see that I already have two\nflights inside of my database.", "start": 4519.38, "duration": 2.58 }, { "text": "I have New York to London\nand New York to Paris.", "start": 4521.96, "duration": 2.46 }, { "text": "I'll add a new one.", "start": 4524.42, "duration": 1.38 }, { "text": "It's letting me choose an origin,\ndestination, and duration.", "start": 4525.8, "duration": 2.88 }, { "text": "And Django knows that the\norigin must be an airport,", "start": 4528.68, "duration": 2.67 }, { "text": "so it's going to give me the\nopportunity to just choose an airport.", "start": 4531.35, "duration": 3.05 }, { "text": "Where I can say, OK,\nShanghai is the origin.", "start": 4534.4, "duration": 2.23 }, { "text": "The destination is going to\nbe Paris, and the duration", "start": 4536.63, "duration": 2.67 }, { "text": "is going to be 760 minutes, for example.", "start": 4539.3, "duration": 3.11 }, { "text": "So now, using Django's\nadmin interface, I've", "start": 4542.41, "duration": 2.27 }, { "text": "been able to add a number\nof different flights", "start": 4544.68, "duration": 2.1 }, { "text": "and a number of different airports.", "start": 4546.78, "duration": 1.65 }, { "text": "And if I go back, not to the\nadmin app, but to my flights app,", "start": 4548.43, "duration": 3.78 }, { "text": "the app that I wrote myself,\nand go back to /flights.", "start": 4552.21, "duration": 2.76 }, { "text": "Now, I actually see\nall of the new flights", "start": 4554.97, "duration": 2.82 }, { "text": "that I have added to my database\nvia Django's admin interface.", "start": 4557.79, "duration": 3.463 }, { "text": "I added them to the admin interface,\nand now I see this flight from Shanghai", "start": 4561.253, "duration": 3.167 }, { "text": "to Paris.", "start": 4564.42, "duration": 0.78 }, { "text": "I see this flight from\nParis to New York as well.", "start": 4565.2, "duration": 4.57 }, { "text": "And so now, what I might\nlike to do is begin", "start": 4569.77, "duration": 2.06 }, { "text": "to add some more pages\nto this web application.", "start": 4571.83, "duration": 2.45 }, { "text": "Make this web application\na little more sophisticated", "start": 4574.28, "duration": 2.86 }, { "text": "by maybe giving me the ability\nto click on a particular flight", "start": 4577.14, "duration": 3.72 }, { "text": "to view details about that flight.", "start": 4580.86, "duration": 1.98 }, { "text": "What I'd like is for every\nflight to have its own page,", "start": 4582.84, "duration": 2.82 }, { "text": "not just /flights for all the flights,\nbut /flight/one for flight ID one.", "start": 4585.66, "duration": 4.86 }, { "text": "/flight/two for ID two,\nso on and so forth.", "start": 4590.52, "duration": 3.72 }, { "text": "What I can do in order to do\nthat is go back into urls.py", "start": 4594.24, "duration": 4.41 }, { "text": "and create a new path.", "start": 4598.65, "duration": 1.44 }, { "text": "We'll create a path where\nI'm going to specify", "start": 4600.09, "duration": 2.67 }, { "text": "a flight ID, which would be an integer.", "start": 4602.76, "duration": 2.73 }, { "text": "When I do, let's go ahead and load the\nflight view, whose name will be flight.", "start": 4605.49, "duration": 4.32 }, { "text": "And now, I need to just go to views.py\nand add a function called flight.", "start": 4609.81, "duration": 4.92 }, { "text": "So I go back, go into views.py.", "start": 4614.73, "duration": 2.34 }, { "text": "In addition to an index function,\nwe'll define a flight function that", "start": 4617.07, "duration": 4.14 }, { "text": "accepts as an argument a flight ID.", "start": 4621.21, "duration": 3.18 }, { "text": "So now, what is the flight\nfunction going to do?", "start": 4624.39, "duration": 2.35 }, { "text": "Well, the first thing I need to\ndo is actually get that flight.", "start": 4626.74, "duration": 3.08 }, { "text": "I can say flight equals\nflight.objects.get.", "start": 4629.82, "duration": 2.81 }, { "text": "Get me the flight whose idea is\nequal to flight ID, for example.", "start": 4632.63, "duration": 6.34 }, { "text": "Or alternatively, Django also\nlet's you say pk instead of ID.", "start": 4638.97, "duration": 3.72 }, { "text": "It's a much more generic\nway of referencing", "start": 4642.69, "duration": 1.8 }, { "text": "the primary key, for whatever the\nprimary key happens to be called.", "start": 4644.49, "duration": 3.36 }, { "text": "The pk in this case is just the ID.", "start": 4647.85, "duration": 2.31 }, { "text": "But then what I can do is render a\ntemplate, like flight/flight.html,", "start": 4650.16, "duration": 6.63 }, { "text": "and pass as input to that the flight.", "start": 4656.79, "duration": 3.63 }, { "text": "So we're passing this\nflight to flight.html.", "start": 4660.42, "duration": 3.87 }, { "text": "And now, I can create a template--", "start": 4664.29, "duration": 2.01 }, { "text": "create a new file called flight.html\nwhich is going to also extend", "start": 4666.3, "duration": 5.73 }, { "text": "flight/layout.html using\nthat same HTML layout.", "start": 4672.03, "duration": 4.06 }, { "text": "And inside the body of\nthe page, let's just", "start": 4676.09, "duration": 3.23 }, { "text": "say something like in\nbig we'll say flight ID.", "start": 4679.32, "duration": 5.55 }, { "text": "And then, maybe an unordered list where\nI can say something like the origin", "start": 4684.87, "duration": 4.05 }, { "text": "is flight.origin.", "start": 4688.92, "duration": 2.43 }, { "text": "The destination is flight.destination.", "start": 4691.35, "duration": 4.46 }, { "text": "And the duration is flight.duration.", "start": 4695.81, "duration": 5.88 }, { "text": "So now, I have a page that\ndisplays flight information", "start": 4701.69, "duration": 2.88 }, { "text": "about any particular flight.", "start": 4704.57, "duration": 1.86 }, { "text": "And if I go ahead and load not /flights\nin my web browser, but /flights/one,", "start": 4706.43, "duration": 5.1 }, { "text": "for example.", "start": 4711.53, "duration": 0.692 }, { "text": "Well, now I have information\nabout flight number one.", "start": 4712.222, "duration": 2.208 }, { "text": "And /flight/two gets me information\nabout flight number two.", "start": 4714.43, "duration": 3.61 }, { "text": "Querying for that particular\nflight, then printing out", "start": 4718.04, "duration": 2.4 }, { "text": "its origin, destination, and duration.", "start": 4720.44, "duration": 3.04 }, { "text": "Now, there is some error checking\nthat we probably should do here.", "start": 4723.48, "duration": 2.75 }, { "text": "If I try and access a\nflight that doesn't exist,", "start": 4726.23, "duration": 2.0 }, { "text": "something like flight 28, for example.", "start": 4728.23, "duration": 2.54 }, { "text": "I'm going to get some sort of\nerror that does not exist error.", "start": 4730.77, "duration": 2.78 }, { "text": "Flight matching query does not exist.", "start": 4733.55, "duration": 2.04 }, { "text": "I might like to control what happens\nin that situation a little better.", "start": 4735.59, "duration": 3.283 }, { "text": "So you might imagine adding\nsome additional error checking", "start": 4738.873, "duration": 2.417 }, { "text": "to handle those cases as well.", "start": 4741.29, "duration": 1.5 }, { "text": "But we'll leave it at this just for now.", "start": 4742.79, "duration": 2.52 }, { "text": "But now, let's go ahead\nand add the ability,", "start": 4745.31, "duration": 2.49 }, { "text": "not only to have flights that have\nairports associated with them,", "start": 4747.8, "duration": 3.24 }, { "text": "but let's also add\npassengers to our flights", "start": 4751.04, "duration": 2.61 }, { "text": "as well to be able to represent\npassengers that might actually", "start": 4753.65, "duration": 3.15 }, { "text": "be on these flights, too.", "start": 4756.8, "duration": 2.06 }, { "text": "So go ahead and go back into models.py.", "start": 4758.86, "duration": 3.83 }, { "text": "And in models.py, in addition to an\nairport class and a flight class,", "start": 4762.69, "duration": 4.43 }, { "text": "let me create a new\nclass called passenger.", "start": 4767.12, "duration": 4.23 }, { "text": "Also going to be a model.", "start": 4771.35, "duration": 1.62 }, { "text": "And what properties\ndoes a passenger have?", "start": 4772.97, "duration": 2.22 }, { "text": "Well, a passenger has\na first name, which", "start": 4775.19, "duration": 1.92 }, { "text": "we'll go ahead and make a\nmodels.CharField whose max length", "start": 4777.11, "duration": 5.34 }, { "text": "we'll put at 64.", "start": 4782.45, "duration": 2.16 }, { "text": "And the last name.", "start": 4784.61, "duration": 2.82 }, { "text": "Max length equals 64.", "start": 4787.43, "duration": 1.95 }, { "text": "And passengers also,\nas we described before,", "start": 4789.38, "duration": 2.91 }, { "text": "they have a many to many\nrelationship with flights.", "start": 4792.29, "duration": 3.435 }, { "text": "That a flight could have\nmultiple passengers,", "start": 4795.725, "duration": 1.875 }, { "text": "a passenger could be\non multiple flights,", "start": 4797.6, "duration": 1.98 }, { "text": "and ultimately, we need an additional\ntable to keep track of this.", "start": 4799.58, "duration": 2.91 }, { "text": "But we can think a little bit\nmore abstractly here in Django", "start": 4802.49, "duration": 2.82 }, { "text": "and just say that every passenger has\nflights associated with them, which are", "start": 4805.31, "duration": 4.62 }, { "text": "a models.manytomanyfield with flight.", "start": 4809.93, "duration": 4.65 }, { "text": "So every passenger could be\nassociated with many flights.", "start": 4814.58, "duration": 3.64 }, { "text": "We'll say blank equals true to allow\nthe possibility that a passenger has", "start": 4818.22, "duration": 4.07 }, { "text": "no flights.", "start": 4822.29, "duration": 0.6 }, { "text": "Maybe if they're not registered\nfor any flights at all.", "start": 4822.89, "duration": 3.25 }, { "text": "And we'll also give this a\nrelated name of passengers,", "start": 4826.14, "duration": 4.01 }, { "text": "meaning if I have a passenger,\nI can use the flights attribute", "start": 4830.15, "duration": 3.81 }, { "text": "to access all of their flights.", "start": 4833.96, "duration": 1.68 }, { "text": "And likewise, if I have a flight, I\ncan use this passenger's related name", "start": 4835.64, "duration": 4.23 }, { "text": "to access all of the passengers\nwho are on that flight.", "start": 4839.87, "duration": 3.06 }, { "text": "And we'll see how that'll\nbe useful in a moment, too.", "start": 4842.93, "duration": 2.73 }, { "text": "The string representation\nof a passenger will just", "start": 4845.66, "duration": 3.09 }, { "text": "go ahead and be their first\nname space their last name,", "start": 4848.75, "duration": 4.05 }, { "text": "which feels like a reasonable way of\nrepresenting a particular passenger.", "start": 4852.8, "duration": 3.81 }, { "text": "And now, I need to apply these changes.", "start": 4856.61, "duration": 2.31 }, { "text": "I need to say, python manage.py.", "start": 4858.92, "duration": 2.16 }, { "text": "Make migrations because I've\nmade new changes to my model.", "start": 4861.08, "duration": 3.36 }, { "text": "I've created a model\npassenger, in particular.", "start": 4864.44, "duration": 2.7 }, { "text": "And now, if I do python\nmanage.py migrate,", "start": 4867.14, "duration": 2.91 }, { "text": "now I've applied those\nchanges to my actual database.", "start": 4870.05, "duration": 3.75 }, { "text": "And if I go into admin.py,\nso we'll go into admin.py,", "start": 4873.8, "duration": 4.86 }, { "text": "and register not only flight\nand airport but passenger,", "start": 4878.66, "duration": 4.8 }, { "text": "admin.site.register passenger,\nthen now via the admin interface,", "start": 4883.46, "duration": 4.56 }, { "text": "I can manipulate passengers as well.", "start": 4888.02, "duration": 2.25 }, { "text": "I can say python manage.py run\nserver to run my web server.", "start": 4890.27, "duration": 5.28 }, { "text": "Go to my web servers admin\nview by going to /admin.", "start": 4895.55, "duration": 4.26 }, { "text": "Go down to passengers, and let's\ngo ahead and add a passenger.", "start": 4899.81, "duration": 4.43 }, { "text": "Where I can say, all\nright, first name Harry,", "start": 4904.24, "duration": 2.08 }, { "text": "last name Potter, and we'll go ahead\nand put him on flight 1 and flight 3,", "start": 4906.32, "duration": 5.11 }, { "text": "maybe.", "start": 4911.43, "duration": 0.5 }, { "text": "He's on two different\nflights, for example.", "start": 4911.93, "duration": 1.88 }, { "text": "And you can hold down command or control\nto be able to select multiple flights.", "start": 4913.81, "duration": 3.55 }, { "text": "And we'll go ahead and save that.", "start": 4917.36, "duration": 1.5 }, { "text": "Harry Potter has been\nadded successfully.", "start": 4918.86, "duration": 1.86 }, { "text": "And let's add a couple\nof other passengers.", "start": 4920.72, "duration": 1.792 }, { "text": "We'll add Ron Weasley.", "start": 4922.512, "duration": 2.018 }, { "text": "And we'll add another.", "start": 4924.53, "duration": 1.08 }, { "text": "We'll add Hermione Granger.", "start": 4925.61, "duration": 2.7 }, { "text": "And we'll add Ginny Weasley as well.", "start": 4928.31, "duration": 3.17 }, { "text": "So we've added a number\nof different passengers", "start": 4931.48, "duration": 1.96 }, { "text": "that now all exist in\nDjango's admin interface.", "start": 4933.44, "duration": 2.94 }, { "text": "And now, what I'd like to\ndo is on the flight page,", "start": 4936.38, "duration": 2.52 }, { "text": "display information\nabout which passengers", "start": 4938.9, "duration": 2.52 }, { "text": "happened to be on any given flight.", "start": 4941.42, "duration": 2.37 }, { "text": "So the way I might do that\nis by going into views.py.", "start": 4943.79, "duration": 5.46 }, { "text": "And on the flight page, in addition\nto giving access to the flight,", "start": 4949.25, "duration": 3.99 }, { "text": "let me also give it\naccess to passengers.", "start": 4953.24, "duration": 2.49 }, { "text": "So passengers this template\nis going to get access to.", "start": 4955.73, "duration": 3.28 }, { "text": "And we get passengers by\nsaying flight.passengers.all.", "start": 4959.01, "duration": 4.66 }, { "text": "And the reason we can do this\nis, again, because passengers", "start": 4963.67, "duration": 3.07 }, { "text": "is that related name.", "start": 4966.74, "duration": 1.29 }, { "text": "It is our way of taking a flight and\ngetting all of the passengers that", "start": 4968.03, "duration": 4.02 }, { "text": "happened to be on that flight.", "start": 4972.05, "duration": 2.7 }, { "text": "And so now, inside of flight.html\nI can add something like,", "start": 4974.75, "duration": 6.48 }, { "text": "let's add an H2 called passengers.", "start": 4981.23, "duration": 2.93 }, { "text": "Where here, I'm going to loop\nfor passenger in passengers.", "start": 4984.16, "duration": 6.41 }, { "text": "Go ahead and display that passenger.", "start": 4990.57, "duration": 4.35 }, { "text": "Just print out that passenger\ninside of a list item.", "start": 4994.92, "duration": 3.13 }, { "text": "And in Django, I can say,\nif the list is empty,", "start": 4998.05, "duration": 2.42 }, { "text": "let's just have a list item\nthat says, no passengers.", "start": 5000.47, "duration": 3.0 }, { "text": "Meaning nobody is\ncurrently on this flight.", "start": 5003.47, "duration": 3.9 }, { "text": "So now, my web server is still running.", "start": 5007.37, "duration": 2.25 }, { "text": "I can go back to /flights.", "start": 5009.62, "duration": 2.08 }, { "text": "Here are all of the flights.", "start": 5011.7, "duration": 2.35 }, { "text": "And if I go to /flight/one I\nnow see that I'm flight one.", "start": 5014.05, "duration": 3.76 }, { "text": "Harry Potter is a\npassenger on that flight.", "start": 5017.81, "duration": 2.35 }, { "text": "But if I go to flight two, all right,\nno passengers are on that flight either.", "start": 5020.16, "duration": 4.013 }, { "text": "And now, it's been a\nlittle annoying that I've", "start": 5024.173, "duration": 1.917 }, { "text": "had to do everything by using\nthe URL here to be able to go", "start": 5026.09, "duration": 3.27 }, { "text": "back and forth between pages.", "start": 5029.36, "duration": 1.65 }, { "text": "I could link to those\npages if I want to.", "start": 5031.01, "duration": 2.55 }, { "text": "And the way I might do that\nis, let's on the flight page,", "start": 5033.56, "duration": 3.48 }, { "text": "add a link that goes to the\nURL index that says something", "start": 5037.04, "duration": 6.21 }, { "text": "like, back to flight list, maybe.", "start": 5043.25, "duration": 3.0 }, { "text": "So here is now a link that\ntakes me to the index view.", "start": 5046.25, "duration": 3.15 }, { "text": "And likewise, I can go into index.html.", "start": 5049.4, "duration": 3.36 }, { "text": "And for each of these list\nitems, each of these list items", "start": 5052.76, "duration": 3.51 }, { "text": "is really going to be a link that links\nto it's a url to a particular flight.", "start": 5056.27, "duration": 7.5 }, { "text": "And the flight route takes\nas a parameter a flight ID.", "start": 5063.77, "duration": 3.87 }, { "text": "And so inside this url\na substitution here.", "start": 5067.64, "duration": 2.88 }, { "text": "I can specify use flight.id\nas the ID of the flight", "start": 5070.52, "duration": 4.74 }, { "text": "that I would like to use here.", "start": 5075.26, "duration": 2.59 }, { "text": "And so now, I've put\nevery single flight inside", "start": 5077.85, "duration": 2.9 }, { "text": "of a link that takes\nme to the flight route.", "start": 5080.75, "duration": 2.7 }, { "text": "But because the flight route requires\nas an argument the flight ID,", "start": 5083.45, "duration": 3.27 }, { "text": "I can specify the flight ID here.", "start": 5086.72, "duration": 3.53 }, { "text": "And so now, if I go back to /flights,\nI now see a list of flights where every", "start": 5090.25, "duration": 4.15 }, { "text": "flight is in fact a link that\ncan take me somewhere else.", "start": 5094.4, "duration": 3.37 }, { "text": "And so now, I can click on any one of\nthose links, like New York to Paris,", "start": 5097.77, "duration": 4.25 }, { "text": "and that takes me to the flight page.", "start": 5102.02, "duration": 1.71 }, { "text": "I can click back to flight lists,\nthat takes me back to the flight list.", "start": 5103.73, "duration": 3.0 }, { "text": "Click on another flight and\ngo to that flight as well.", "start": 5106.73, "duration": 2.59 }, { "text": "So I've now been able to come\nup with this way of linking", "start": 5109.32, "duration": 3.5 }, { "text": "these pages together by having links\nin each of the various different pages", "start": 5112.82, "duration": 3.57 }, { "text": "that take me to some\nother route as well.", "start": 5116.39, "duration": 4.3 }, { "text": "And so now what I might\nlike to do is, in addition", "start": 5120.69, "duration": 2.09 }, { "text": "to displaying all the passengers\non any particular flight,", "start": 5122.78, "duration": 2.73 }, { "text": "also give myself the ability\nto add passengers to a flight", "start": 5125.51, "duration": 3.24 }, { "text": "as well, which feels\nlike a reasonable thing", "start": 5128.75, "duration": 2.01 }, { "text": "that I might want to do inside\nof this web application.", "start": 5130.76, "duration": 3.07 }, { "text": "And so how can I go about doing that?", "start": 5133.83, "duration": 2.117 }, { "text": "Well, in order to do\nthat, I'm going to need", "start": 5135.947, "duration": 1.833 }, { "text": "some new route that lets me book a\nflight for a particular passenger.", "start": 5137.78, "duration": 4.55 }, { "text": "And so I'll go ahead\nand go back to urls.py.", "start": 5142.33, "duration": 3.28 }, { "text": "And inside of urls.py I'll add a new\npath that will be int flightid/book.", "start": 5145.61, "duration": 8.82 }, { "text": "And int flightid/book\nis going to let me book", "start": 5154.43, "duration": 2.25 }, { "text": "a flight for this particular flight ID.", "start": 5156.68, "duration": 3.0 }, { "text": "For flight one or flight two\nor flight three, or so forth.", "start": 5159.68, "duration": 3.81 }, { "text": "When I do, we'll go ahead and go to the\nbook view, and we'll name that book.", "start": 5163.49, "duration": 5.07 }, { "text": "And so now, I need to\nimplement the book view.", "start": 5168.56, "duration": 3.673 }, { "text": "So how is this view going to work?", "start": 5172.233, "duration": 1.417 }, { "text": "I'm going to define a\nfunction called book", "start": 5173.65, "duration": 3.64 }, { "text": "that is going to take as its\nargument, not only the request,", "start": 5177.29, "duration": 3.78 }, { "text": "but also a flight ID.", "start": 5181.07, "duration": 1.89 }, { "text": "The first thing, as with before,\nis I want to get the flight ID.", "start": 5182.96, "duration": 3.15 }, { "text": "But remember from before,\nthat there are multiple ways", "start": 5186.11, "duration": 2.43 }, { "text": "that I can request a web page.", "start": 5188.54, "duration": 2.07 }, { "text": "I can request a web page\nvia the get request method,", "start": 5190.61, "duration": 2.86 }, { "text": "which means I would just\nlike to get this page.", "start": 5193.47, "duration": 2.48 }, { "text": "Or I can request the\nmethod via post, meaning", "start": 5195.95, "duration": 2.19 }, { "text": "I would like to send data to the page.", "start": 5198.14, "duration": 2.19 }, { "text": "And generally speaking, anytime you want\nto manipulate the state of something,", "start": 5200.33, "duration": 4.24 }, { "text": "especially manipulating\nour database, that", "start": 5204.57, "duration": 2.12 }, { "text": "should be inside of a post request.", "start": 5206.69, "duration": 2.31 }, { "text": "I'm submitting some form, some data.", "start": 5209.0, "duration": 2.16 }, { "text": "And in response to that\npost submission, you", "start": 5211.16, "duration": 2.25 }, { "text": "should manipulate what's going\non inside of the database.", "start": 5213.41, "duration": 3.69 }, { "text": "So we're going to check when\nthis book route is called upon.", "start": 5217.1, "duration": 3.9 }, { "text": "If request method is post, then we\nwant to perform some sort of action.", "start": 5221.0, "duration": 5.31 }, { "text": "The flight in question is just\ngoing to be flight.objects.get.", "start": 5226.31, "duration": 4.86 }, { "text": "Get the flight whose primary\nkey is that flight ID.", "start": 5231.17, "duration": 4.29 }, { "text": "And then, what I'd also like to\ndo is associated with the form.", "start": 5235.46, "duration": 3.48 }, { "text": "When someone submits this form to\nbook a new passenger on the flight,", "start": 5238.94, "duration": 3.18 }, { "text": "they should tell me what\nthe ID is of the passenger.", "start": 5242.12, "duration": 3.66 }, { "text": "What passenger should\nI book on the flight?", "start": 5245.78, "duration": 1.972 }, { "text": "Because those are the\ntwo pieces of information", "start": 5247.752, "duration": 1.958 }, { "text": "you need to know in order\nto actually book a flight.", "start": 5249.71, "duration": 2.28 }, { "text": "You need the flight and\nthe passenger information.", "start": 5251.99, "duration": 3.04 }, { "text": "So let's assume for now\nthat the information is", "start": 5255.03, "duration": 2.48 }, { "text": "going to be in request.post and\nthen in square brackets passenger.", "start": 5257.51, "duration": 5.37 }, { "text": "What this means is that the\ndata about which passenger", "start": 5262.88, "duration": 3.06 }, { "text": "ID we want to register\non this flight is going", "start": 5265.94, "duration": 3.63 }, { "text": "to be passed in via a form with an\ninput field whose name is passenger.", "start": 5269.57, "duration": 4.59 }, { "text": "The name on any particular input\nfield dictates what name we get--", "start": 5274.16, "duration": 4.24 }, { "text": "is received when a route\nlike this book route", "start": 5278.4, "duration": 2.66 }, { "text": "is able to process the\nrequest from the user.", "start": 5281.06, "duration": 3.16 }, { "text": "So we'll go ahead and\ntake that information.", "start": 5284.22, "duration": 1.9 }, { "text": "And because by default this\nmight be a string, let's go ahead", "start": 5286.12, "duration": 3.43 }, { "text": "and convert it into an\ninteger just to make sure", "start": 5289.55, "duration": 2.4 }, { "text": "we're dealing with an integer.", "start": 5291.95, "duration": 1.65 }, { "text": "And let me say that the\npassenger in question", "start": 5293.6, "duration": 2.61 }, { "text": "is going to be passenger.objects.get\npk equals this whole thing.", "start": 5296.21, "duration": 7.89 }, { "text": "So now what I've done\nis, if the request method", "start": 5304.1, "duration": 2.73 }, { "text": "is post, meaning someone submitted\nthis form via the post request method,", "start": 5306.83, "duration": 4.05 }, { "text": "I'm first thing flights.objects.get.\nto get a particular flight,", "start": 5310.88, "duration": 3.81 }, { "text": "get me the flight with that flight ID.", "start": 5314.69, "duration": 2.55 }, { "text": "And then, I'm getting a passenger.", "start": 5317.24, "duration": 1.77 }, { "text": "Which passenger am I getting?", "start": 5319.01, "duration": 1.5 }, { "text": "The one who's pk, their primary\nkey, otherwise known as ID,", "start": 5320.51, "duration": 3.36 }, { "text": "is equal to whatever was\nsubmitted via this post form", "start": 5323.87, "duration": 3.96 }, { "text": "with a name of passenger.", "start": 5327.83, "duration": 1.89 }, { "text": "And we haven't yet created that form,\nbut we'll do so in just a moment.", "start": 5329.72, "duration": 3.97 }, { "text": "Now ultimately, we'll want to add some\nmore error checking to this as well,", "start": 5333.69, "duration": 3.29 }, { "text": "like what if someone requests\na passenger that doesn't exist,", "start": 5336.98, "duration": 2.82 }, { "text": "or a flight that doesn't exist either.", "start": 5339.8, "duration": 1.7 }, { "text": "So there is definitely\nsome error checking", "start": 5341.5, "duration": 1.75 }, { "text": "that we probably should be doing here.", "start": 5343.25, "duration": 1.78 }, { "text": "But for simplicity, let's just assume\nfor now that we're able to get a flight", "start": 5345.03, "duration": 4.01 }, { "text": "and get a passenger.", "start": 5349.04, "duration": 1.62 }, { "text": "Well, how do we access\na passenger's flights?", "start": 5350.66, "duration": 2.43 }, { "text": "I can just say passenger.flights.", "start": 5353.09, "duration": 3.45 }, { "text": "And in order to add a new\nitem to some set like flights,", "start": 5356.54, "duration": 3.6 }, { "text": "I can just say\npassenger.flights.add flight.", "start": 5360.14, "duration": 3.992 }, { "text": "And this will do the\nequivalent of adding", "start": 5364.132, "duration": 1.708 }, { "text": "a new row into a table of keeping track\nthat the passengers on that flight.", "start": 5365.84, "duration": 3.9 }, { "text": "But the nice thing about\nDjango's abstractions", "start": 5369.74, "duration": 2.19 }, { "text": "is that I don't have to worry\nabout those underlying details.", "start": 5371.93, "duration": 2.67 }, { "text": "I don't have to worry about what\nthe structures of the tables are.", "start": 5374.6, "duration": 2.82 }, { "text": "I can think at a much higher level\nand just say take this passenger,", "start": 5377.42, "duration": 4.44 }, { "text": "take their set of flights, and\ngo ahead and add a new flight", "start": 5381.86, "duration": 3.42 }, { "text": "to that set of flights.", "start": 5385.28, "duration": 2.54 }, { "text": "And when all that's said and\ndone, what I probably want to do", "start": 5387.82, "duration": 3.43 }, { "text": "is return some sort of redirect\nthat redirects the user back", "start": 5391.25, "duration": 4.17 }, { "text": "to the flight page.", "start": 5395.42, "duration": 1.14 }, { "text": "So we'll go ahead and return\nan HTTP response redirect.", "start": 5396.56, "duration": 4.65 }, { "text": "What you URL would I\nlike to take them to?", "start": 5401.21, "duration": 1.96 }, { "text": "Well, I'd like to take\nthem to the flight route.", "start": 5403.17, "duration": 3.53 }, { "text": "And reverse, again, takes the\nname of a particular view,", "start": 5406.7, "duration": 3.18 }, { "text": "and gets me what the URL is,\nand we saw that last time.", "start": 5409.88, "duration": 3.3 }, { "text": "And the flight route takes an argument.", "start": 5413.18, "duration": 1.75 }, { "text": "So I need to pass as an\nargument the flight's ID.", "start": 5414.93, "duration": 3.688 }, { "text": "So I need to provide\nit to the flight route", "start": 5418.618, "duration": 1.792 }, { "text": "what the flight's ID is,\nstructured it as a tuple,", "start": 5420.41, "duration": 3.21 }, { "text": "and that is going to redirect\nme back to the flight route", "start": 5423.62, "duration": 3.6 }, { "text": "so that I can see that\nflight page again.", "start": 5427.22, "duration": 3.57 }, { "text": "And what I need to add up at the\ntop here is from django.http.", "start": 5430.79, "duration": 4.95 }, { "text": "Import HttpResponseRedirect in addition\nto from django.urls import reverse.", "start": 5435.74, "duration": 11.63 }, { "text": "And so those I'll need\nto add as well so that I", "start": 5447.37, "duration": 2.18 }, { "text": "can redirect the user\nback to the flight page", "start": 5449.55, "duration": 2.67 }, { "text": "after they're done submitting the form.", "start": 5452.22, "duration": 1.68 }, { "text": "And reverse takes the\nname of a particular view,", "start": 5453.9, "duration": 3.21 }, { "text": "as defined in urls.py, something\nlike index or flight or book,", "start": 5457.11, "duration": 4.11 }, { "text": "and gets me what the\nactual URL path should be.", "start": 5461.22, "duration": 3.1 }, { "text": "And as we talked about\nlast time, that's helpful", "start": 5464.32, "duration": 2.0 }, { "text": "so that I don't have to hard code\nURLs into my Django web application.", "start": 5466.32, "duration": 4.02 }, { "text": "I can just reference URLs by their name.", "start": 5470.34, "duration": 2.28 }, { "text": "And if ever I need to\nchange a URL, I can just", "start": 5472.62, "duration": 2.25 }, { "text": "change it in one place in\nurls.py, and that change", "start": 5474.87, "duration": 3.72 }, { "text": "is going to reflect\neverywhere else as well.", "start": 5478.59, "duration": 3.9 }, { "text": "So now, the next thing I need to\ndo is actually create this form.", "start": 5482.49, "duration": 3.89 }, { "text": "That what I have so\nfar is just a function", "start": 5486.38, "duration": 2.23 }, { "text": "called book that is waiting for\na post request to be made to it.", "start": 5488.61, "duration": 3.63 }, { "text": "And when a post request\nis made to it, then we're", "start": 5492.24, "duration": 2.22 }, { "text": "going to go ahead and submit this\nform and go ahead and add the flight", "start": 5494.46, "duration": 3.39 }, { "text": "for this particular passenger.", "start": 5497.85, "duration": 1.68 }, { "text": "But what I'd like to do now\nis actually add that form.", "start": 5499.53, "duration": 2.83 }, { "text": "So I'll go back into\ntemplates, go into flight.html,", "start": 5502.36, "duration": 4.32 }, { "text": "and what I'd like to add here is a form.", "start": 5506.68, "duration": 2.57 }, { "text": "I'll go ahead and label it with\nan H2 called add passenger.", "start": 5509.25, "duration": 3.93 }, { "text": "And we'll create a form whose\naction is going to be URL of book.", "start": 5513.18, "duration": 7.04 }, { "text": "So we're going to go to the book route.", "start": 5520.22, "duration": 1.72 }, { "text": "And again, if we recall\nthe book route in urls.py,", "start": 5521.94, "duration": 2.77 }, { "text": "the route with name book, this view,\nrequires as a parameter some flight ID.", "start": 5524.71, "duration": 5.54 }, { "text": "So I need to provide the flight\nID as an argument for what flight", "start": 5530.25, "duration": 3.81 }, { "text": "I'm booking the passenger on.", "start": 5534.06, "duration": 1.86 }, { "text": "And it just so happens to be\nflight.id because this template has", "start": 5535.92, "duration": 2.97 }, { "text": "access to a variable called flight.", "start": 5538.89, "duration": 2.84 }, { "text": "The method of this submission\nis, again, going to be post.", "start": 5541.73, "duration": 3.67 }, { "text": "And recall from before that\nwhenever I have a form in Django,", "start": 5545.4, "duration": 2.76 }, { "text": "I need to give it the CSRF token\njust for security to make sure", "start": 5548.16, "duration": 3.45 }, { "text": "that Django knows it's\nreally this application that", "start": 5551.61, "duration": 2.55 }, { "text": "is submitting this form.", "start": 5554.16, "duration": 1.56 }, { "text": "We'll go ahead and add a\ndropdown list, which you can", "start": 5555.72, "duration": 2.49 }, { "text": "create in HTML using a select field.", "start": 5558.21, "duration": 3.06 }, { "text": "The name of this select field\nis going to be passenger.", "start": 5561.27, "duration": 4.14 }, { "text": "And the reason for that is inside of\nviews.py, when I get the passenger,", "start": 5565.41, "duration": 4.56 }, { "text": "I'm looking for inside the post\ndata, inside of request.post,", "start": 5569.97, "duration": 3.6 }, { "text": "for a field whose name is passenger.", "start": 5573.57, "duration": 2.31 }, { "text": "And so that is what I would like\nthe name of this dropdown to be.", "start": 5575.88, "duration": 3.77 }, { "text": "And inside of a select dropdown,\nwe have a whole bunch of options.", "start": 5579.65, "duration": 3.46 }, { "text": "Options that we can choose from.", "start": 5583.11, "duration": 1.57 }, { "text": "And there's going to be\none option for everyone who", "start": 5584.68, "duration": 2.72 }, { "text": "isn't a passenger on this flight.", "start": 5587.4, "duration": 3.3 }, { "text": "And so how do I get everyone who\nisn't a passenger on the flight?", "start": 5590.7, "duration": 3.15 }, { "text": "Well, it seems that right now,\nthe flight page only has access", "start": 5593.85, "duration": 3.3 }, { "text": "to actual passengers, and\ndoesn't yet have access to people", "start": 5597.15, "duration": 3.87 }, { "text": "that are not passengers on the flight.", "start": 5601.02, "duration": 2.4 }, { "text": "So it sounds like I need to add some\nadditional context to this template.", "start": 5603.42, "duration": 3.57 }, { "text": "Additional information\nthat we want access to.", "start": 5606.99, "duration": 2.88 }, { "text": "So I'll go ahead and give this flight\naccess to additional information", "start": 5609.87, "duration": 3.39 }, { "text": "that we'll call non-passengers.", "start": 5613.26, "duration": 2.28 }, { "text": "For people that are not on the flight.", "start": 5615.54, "duration": 2.46 }, { "text": "And how do we I non-passengers?", "start": 5618.0, "duration": 1.92 }, { "text": "Well, just as I could say\npassenger.objects.filter", "start": 5619.92, "duration": 3.72 }, { "text": "to only get passengers that\nmatch a particular query,", "start": 5623.64, "duration": 3.61 }, { "text": "there's also a way in Django to say\npassenger.objects.exclude to say", "start": 5627.25, "duration": 4.73 }, { "text": "exclude passengers that\nsatisfy a particular query.", "start": 5631.98, "duration": 3.82 }, { "text": "So I want to exclude passengers\nwho, among their flights,", "start": 5635.8, "duration": 3.65 }, { "text": "have this as one of their flights.", "start": 5639.45, "duration": 3.15 }, { "text": "And so what does this actually mean?", "start": 5642.6, "duration": 1.69 }, { "text": "Well, it means that when\nI render flight.html,", "start": 5644.29, "duration": 2.63 }, { "text": "there's a couple pieces of\ninformation that it should have.", "start": 5646.92, "duration": 2.61 }, { "text": "It needs to know what\nflight is being rendered.", "start": 5649.53, "duration": 2.34 }, { "text": "It needs to know who is on the\nflight, who are the passengers.", "start": 5651.87, "duration": 3.13 }, { "text": "But if I want a dropdown where\nI can choose from all the people", "start": 5655.0, "duration": 2.69 }, { "text": "who aren't already on\nthe flight, like I would", "start": 5657.69, "duration": 2.13 }, { "text": "like to register you for\nthe flight, well, I also", "start": 5659.82, "duration": 2.49 }, { "text": "need all of the non-passengers.", "start": 5662.31, "duration": 1.53 }, { "text": "Passengers except, excluding the\nones who are already on the flight,", "start": 5663.84, "duration": 5.52 }, { "text": "and get me all of them is what\nthat .all is ultimately saying.", "start": 5669.36, "duration": 4.2 }, { "text": "And so using that, I now\nhave access to this variable", "start": 5673.56, "duration": 3.27 }, { "text": "called non-passengers that I can\nuse as I'm constructing this page.", "start": 5676.83, "duration": 5.07 }, { "text": "So back on flight.html, I can say,\nfor every passenger in non-passengers,", "start": 5681.9, "duration": 7.74 }, { "text": "let me create a option\nthat I can choose from.", "start": 5689.64, "duration": 4.6 }, { "text": "And the options value is going\nto be the passenger's ID.", "start": 5694.24, "duration": 5.02 }, { "text": "Because ultimately, when I submit\nthe form, what I care about getting", "start": 5699.26, "duration": 3.42 }, { "text": "is what is the ID of this passenger\nthat I've chosen from this dropdown.", "start": 5702.68, "duration": 4.145 }, { "text": "But of course, the user\nwho's looking at this page,", "start": 5706.825, "duration": 2.125 }, { "text": "they don't want to see people's IDs.", "start": 5708.95, "duration": 1.59 }, { "text": "They want to see people's names.", "start": 5710.54, "duration": 1.74 }, { "text": "So inside of the option\ntag, we'll go ahead", "start": 5712.28, "duration": 2.7 }, { "text": "and just print out the passenger's name.", "start": 5714.98, "duration": 2.66 }, { "text": "And we'll see in a\nmoment what all of this", "start": 5717.64, "duration": 1.75 }, { "text": "actually looks like in terms of HTML.", "start": 5719.39, "duration": 2.47 }, { "text": "So now that I've created\nthis form, I'll also", "start": 5721.86, "duration": 2.54 }, { "text": "need at the bottom to add an\ninput whose type is submit", "start": 5724.4, "duration": 3.12 }, { "text": "to let myself submit this form to.", "start": 5727.52, "duration": 2.94 }, { "text": "Let's now try running this application.", "start": 5730.46, "duration": 2.63 }, { "text": "It looks like there's\na slight error where", "start": 5733.09, "duration": 1.75 }, { "text": "I said view.book instead of views.book.", "start": 5734.84, "duration": 2.063 }, { "text": "Views is the name of the module,\nsince it's in a file called views.py.", "start": 5736.903, "duration": 2.917 }, { "text": "And now, it looks like\nmy server is running OK.", "start": 5739.82, "duration": 3.54 }, { "text": "I can go back to /flights.", "start": 5743.36, "duration": 2.07 }, { "text": "Let me get one of the flights, like\nNew York to London, flight number one.", "start": 5745.43, "duration": 3.53 }, { "text": "And all right.", "start": 5748.96, "duration": 0.64 }, { "text": "Name error.", "start": 5749.6, "duration": 0.99 }, { "text": "Name passenger is not defined.", "start": 5750.59, "duration": 1.56 }, { "text": "This is how Django renders to me errors\nthat are occurring in my Python code.", "start": 5752.15, "duration": 3.63 }, { "text": "Looks like it just means inside of\nveiws.py I'm referencing passenger,", "start": 5755.78, "duration": 4.65 }, { "text": "but I never imported it.", "start": 5760.43, "duration": 1.02 }, { "text": "So up at the top, I'll go ahead\nand import passenger as well.", "start": 5761.45, "duration": 4.0 }, { "text": "Now, it seems that my web\napplication is working OK.", "start": 5765.45, "duration": 2.3 }, { "text": "So now, hopefully, I refresh this\npage, I see flight information.", "start": 5767.75, "duration": 4.09 }, { "text": "I see passengers.", "start": 5771.84, "duration": 1.12 }, { "text": "And I also down at the bottom\nnow see an add passenger section", "start": 5772.96, "duration": 3.79 }, { "text": "with a dropdown list.", "start": 5776.75, "duration": 1.15 }, { "text": "Where I can click on it\nand see, all right, here", "start": 5777.9, "duration": 2.0 }, { "text": "are the three people that are\nnot already on this flight.", "start": 5779.9, "duration": 3.45 }, { "text": "And so if I want to add\nGinny Weasley to this flight,", "start": 5783.35, "duration": 3.27 }, { "text": "I can click Ginny Weasley, click\nsubmit, and that submits the form.", "start": 5786.62, "duration": 3.84 }, { "text": "And I'm redirected back\nto the same flight page.", "start": 5790.46, "duration": 2.49 }, { "text": "Now, Harry and Ginny\nare both on the flight.", "start": 5792.95, "duration": 2.16 }, { "text": "And in the add passenger list, I see\nRon and Hermione as the options for me", "start": 5795.11, "duration": 5.33 }, { "text": "there.", "start": 5800.44, "duration": 1.34 }, { "text": "And so using Django's models, I've\nbeen able to very quickly build up", "start": 5801.78, "duration": 3.08 }, { "text": "a reasonably sophisticated application.", "start": 5804.86, "duration": 1.89 }, { "text": "An application that has models, that\ndisplays that information to me,", "start": 5806.75, "duration": 3.66 }, { "text": "and lets me manipulate that data.", "start": 5810.41, "duration": 1.62 }, { "text": "And that data is ultimately\nstored inside of a SQL database.", "start": 5812.03, "duration": 4.21 }, { "text": "And one of the big powers of\nDjango that it really gives to me", "start": 5816.24, "duration": 2.87 }, { "text": "is this admin interface\nthat I ordinarily", "start": 5819.11, "duration": 3.03 }, { "text": "might have had to spend a lot of time\ndesigning a web interface that just", "start": 5822.14, "duration": 3.39 }, { "text": "lets me do things like take some\nperson and go ahead and update", "start": 5825.53, "duration": 4.11 }, { "text": "what is their name, what\nflights are they on,", "start": 5829.64, "duration": 2.04 }, { "text": "and the ability to very quickly\nadd and delete and edit the models", "start": 5831.68, "duration": 3.88 }, { "text": "is something that in a web application\ncould take quite a lot of time", "start": 5835.56, "duration": 3.32 }, { "text": "to be able to build from scratch.", "start": 5838.88, "duration": 1.56 }, { "text": "But Django, very fortunately,\ngives all of that right to me.", "start": 5840.44, "duration": 3.84 }, { "text": "And this admin interface, even\nthough it is designed by Django,", "start": 5844.28, "duration": 3.03 }, { "text": "it's very customizable\nin terms of things", "start": 5847.31, "duration": 1.86 }, { "text": "that I can do on this admin interface if\nI want to manipulate it in certain ways", "start": 5849.17, "duration": 4.47 }, { "text": "in order to add additional\nfeatures to it as well.", "start": 5853.64, "duration": 3.22 }, { "text": "So we'll see a couple of\nbrief examples of this.", "start": 5856.86, "duration": 2.21 }, { "text": "If I go into admin.py,\nhere's my configuration", "start": 5859.07, "duration": 3.96 }, { "text": "for Django's admin interface.", "start": 5863.03, "duration": 1.97 }, { "text": "And I can say, I would\nlike to configure the admin", "start": 5865.0, "duration": 2.92 }, { "text": "interface in a particular way.", "start": 5867.92, "duration": 1.83 }, { "text": "That in a flight, for\nexample, by default all I saw", "start": 5869.75, "duration": 3.06 }, { "text": "was the flight's origin and destination.", "start": 5872.81, "duration": 2.22 }, { "text": "If I want to be able to see\nmore information about a flight,", "start": 5875.03, "duration": 2.58 }, { "text": "I can say, go ahead and give me a\nclass called flight admin, which is", "start": 5877.61, "duration": 4.26 }, { "text": "going to be a subclass of model admin.", "start": 5881.87, "duration": 3.54 }, { "text": "Where I can specify\nany particular settings", "start": 5885.41, "duration": 2.76 }, { "text": "that I want to apply to how the\nflight admin page is displayed.", "start": 5888.17, "duration": 4.3 }, { "text": "So I can-- and all of this is\ndocumented on Django's website.", "start": 5892.47, "duration": 2.56 }, { "text": "And you just have to read it to be able\nto know what configuration options are", "start": 5895.03, "duration": 3.25 }, { "text": "available to you.", "start": 5898.28, "duration": 1.18 }, { "text": "But I can say, in the list display,\nwhen you list all the flights", "start": 5899.46, "duration": 3.2 }, { "text": "and display them all to me, what\nfield should I have access to?", "start": 5902.66, "duration": 3.39 }, { "text": "Well, go ahead and show me\nsomething like the origin", "start": 5906.05, "duration": 3.45 }, { "text": "and the destination and the duration and\nmaybe also show me the ID, for example.", "start": 5909.5, "duration": 6.03 }, { "text": "So I want to see all of this\ninformation when you load a flight.", "start": 5915.53, "duration": 3.42 }, { "text": "And when I register the flight,\nI'll say register this flight,", "start": 5918.95, "duration": 3.09 }, { "text": "but use the flight admin\nsettings when you do so.", "start": 5922.04, "duration": 2.93 }, { "text": "So I can specify, I would like\nto use these particular settings", "start": 5924.97, "duration": 3.61 }, { "text": "when you view the admin interface.", "start": 5928.58, "duration": 2.85 }, { "text": "And so now, if I go back and\ngo ahead and click on flights,", "start": 5931.43, "duration": 3.78 }, { "text": "then now in this list\ndisplay, whereas before I only", "start": 5935.21, "duration": 3.03 }, { "text": "saw IDs and origins\nand destinations, now I", "start": 5938.24, "duration": 2.52 }, { "text": "can configure it to show me all of\nthe IDs and origins and destinations", "start": 5940.76, "duration": 3.75 }, { "text": "and durations as well.", "start": 5944.51, "duration": 1.38 }, { "text": "I've been able to configure this display\nto work the way I would like it to.", "start": 5945.89, "duration": 3.96 }, { "text": "And there are other\nconfigurations you can do as well.", "start": 5949.85, "duration": 2.4 }, { "text": "One that I quite like to use is if\nI want to update my passenger admin,", "start": 5952.25, "duration": 3.84 }, { "text": "so when I'm editing a\npassenger, you can have", "start": 5956.09, "duration": 3.12 }, { "text": "a special way of manipulating\nmany to many relationships inside", "start": 5959.21, "duration": 3.57 }, { "text": "of an attribute called\nfilter horizontal.", "start": 5962.78, "duration": 2.85 }, { "text": "And if I use a horizontal\nfilter on flights,", "start": 5965.63, "duration": 3.48 }, { "text": "this will just make it a little\nbit nicer for manipulating", "start": 5969.11, "duration": 2.79 }, { "text": "the flights that a passenger is on.", "start": 5971.9, "duration": 1.62 }, { "text": "And again, the specific syntax of\nthis not as important as the idea", "start": 5973.52, "duration": 3.06 }, { "text": "that these are all just configurable\nsettings that Django has documented.", "start": 5976.58, "duration": 3.27 }, { "text": "That you can look at to see how to\nconfigure the Admin interface to work", "start": 5979.85, "duration": 3.0 }, { "text": "exactly the way you want it to work.", "start": 5982.85, "duration": 2.85 }, { "text": "And so now, if I go back\nhome and go to passengers,", "start": 5985.7, "duration": 2.99 }, { "text": "and maybe click on a\npassenger like Harry Potter,", "start": 5988.69, "duration": 2.58 }, { "text": "I now see this horizontal\nfilter, which is", "start": 5991.27, "duration": 2.28 }, { "text": "a very nice way of being\nable to manipulate flights", "start": 5993.55, "duration": 2.82 }, { "text": "that the person is on.", "start": 5996.37, "duration": 1.17 }, { "text": "I see on the left a list\nof their available flights", "start": 5997.54, "duration": 2.16 }, { "text": "that I could add them to.", "start": 5999.7, "duration": 1.47 }, { "text": "On the right, a list of\ntheir chosen flights.", "start": 6001.17, "duration": 1.98 }, { "text": "Flights that they're already on.", "start": 6003.15, "duration": 1.53 }, { "text": "And it becomes very easy for me to just\ntake a flight and double click on it", "start": 6004.68, "duration": 3.75 }, { "text": "to move it from an available flight to\na flight that they're on and vise versa.", "start": 6008.43, "duration": 4.05 }, { "text": "Just very quickly being able to\ncontrol and manipulate these models.", "start": 6012.48, "duration": 3.57 }, { "text": "And this is all stuff that Django just\ngives to you right out of the box.", "start": 6016.05, "duration": 4.89 }, { "text": "So Django now has given\nus a lot of features.", "start": 6020.94, "duration": 2.32 }, { "text": "The ability to represent\nmodels very succinctly.", "start": 6023.26, "duration": 2.6 }, { "text": "A migration method for being\nable to very quickly apply", "start": 6025.86, "duration": 2.64 }, { "text": "those changes to our database.", "start": 6028.5, "duration": 1.92 }, { "text": "And the last thing we'll take a look\nat is this idea of authentication.", "start": 6030.42, "duration": 3.668 }, { "text": "That on many websites, we want\nsome method of authentication.", "start": 6034.088, "duration": 2.542 }, { "text": "Some ability for users to be\nable to log in and log out.", "start": 6036.63, "duration": 3.15 }, { "text": "For Django to remember who a\nparticular user happens to be.", "start": 6039.78, "duration": 3.57 }, { "text": "And what we're going\nto do now is introduce", "start": 6043.35, "duration": 1.83 }, { "text": "an application that lets us interact\nwith this authentication method.", "start": 6045.18, "duration": 3.7 }, { "text": "Because Django has a whole\nbunch of authentication features", "start": 6048.88, "duration": 2.78 }, { "text": "built right into the framework\nthat we can take advantage of so", "start": 6051.66, "duration": 2.87 }, { "text": "that we don't need to rewrite all the\nlogic for how do you log someone in,", "start": 6054.53, "duration": 3.55 }, { "text": "and what does it mean\nto represent the user.", "start": 6058.08, "duration": 1.89 }, { "text": "Django has done a whole\nlot of that for us.", "start": 6059.97, "duration": 2.7 }, { "text": "So we'll go ahead and create\nan application to do that now.", "start": 6062.67, "duration": 3.1 }, { "text": "All right, so let's go\nback into my terminal now.", "start": 6065.77, "duration": 2.18 }, { "text": "And now, I have this\nairline project inside", "start": 6067.95, "duration": 2.01 }, { "text": "of which is one app called flights.", "start": 6069.96, "duration": 1.86 }, { "text": "And I'd like to now create another app\nthat's going to maintain users inside", "start": 6071.82, "duration": 3.18 }, { "text": "of this application.", "start": 6075.0, "duration": 1.3 }, { "text": "So I'll go ahead and run python\nmanage.py start app users.", "start": 6076.3, "duration": 3.89 }, { "text": "Which will just be an app that's\ngoing to allow me to represent users.", "start": 6080.19, "duration": 3.63 }, { "text": "As before, when I create\na new application,", "start": 6083.82, "duration": 4.01 }, { "text": "I'll need to go into settings.py.", "start": 6087.83, "duration": 2.08 }, { "text": "Add users as one of the installed\napps inside of this project.", "start": 6089.91, "duration": 5.46 }, { "text": "And I'll go into urls.py to say\nI'd also like when I go to users,", "start": 6095.37, "duration": 6.44 }, { "text": "we'll go ahead and include users.urls.", "start": 6101.81, "duration": 4.0 }, { "text": "So all the URLs that are associated\nwith my user's application.", "start": 6105.81, "duration": 3.75 }, { "text": "Now, I'll need to actually\ncreate those URLs.", "start": 6109.56, "duration": 2.51 }, { "text": "So I'll go ahead and go down\ninto my users application,", "start": 6112.07, "duration": 3.05 }, { "text": "create a new file called\nurls.py, inside of which", "start": 6115.12, "duration": 3.74 }, { "text": "is the same as what we've normally\nsee inside of these URLs files.", "start": 6118.86, "duration": 3.15 }, { "text": "I need to import path, import my views,\nand then define some URL patterns.", "start": 6122.01, "duration": 4.125 }, { "text": "Where here what I'd like to do is define\none path that takes me to views.index.", "start": 6128.75, "duration": 7.4 }, { "text": "And we'll call this one index.", "start": 6136.15, "duration": 2.01 }, { "text": "Then I'll create another path that takes\nme to log in, called the log in view.", "start": 6138.16, "duration": 6.01 }, { "text": "And the name will be log in.", "start": 6144.17, "duration": 2.36 }, { "text": "And we'll have another path called\nlog out for a function called log out", "start": 6146.53, "duration": 4.85 }, { "text": "view that will be associated with it.", "start": 6151.38, "duration": 2.258 }, { "text": "So we'll effectively have\nthree different routes.", "start": 6153.638, "duration": 2.042 }, { "text": "One main index route that's just\ngoing to display information about the", "start": 6155.68, "duration": 3.09 }, { "text": "currently signed in user.", "start": 6158.77, "duration": 1.6 }, { "text": "One route for logging\nsomeone in, a form that", "start": 6160.37, "duration": 1.94 }, { "text": "will display the place where\nthey can type in a user name", "start": 6162.31, "duration": 2.4 }, { "text": "and password to log in.", "start": 6164.71, "duration": 1.26 }, { "text": "And then, one route to allow users to\nbe able to log out from this application", "start": 6165.97, "duration": 4.08 }, { "text": "as well.", "start": 6170.05, "duration": 1.285 }, { "text": "So let's go ahead now and\nactually write these functions.", "start": 6171.335, "duration": 2.375 }, { "text": "We need one function\ncalled index, one function", "start": 6173.71, "duration": 2.07 }, { "text": "called log in view, one\nfunction called log out view.", "start": 6175.78, "duration": 3.57 }, { "text": "So we'll go into views.py,\nand we'll start with index.", "start": 6179.35, "duration": 4.22 }, { "text": "And so what does the\nindex function need to do?", "start": 6183.57, "duration": 1.96 }, { "text": "It's going to display information\nabout the currently signed in user.", "start": 6185.53, "duration": 3.84 }, { "text": "That I sign into this website, and\nthen I'm presented with the index page.", "start": 6189.37, "duration": 3.83 }, { "text": "Because if we think about\nthis programmatically,", "start": 6193.2, "duration": 2.14 }, { "text": "we first need to think\nabout what should happen", "start": 6195.34, "duration": 2.4 }, { "text": "if someone tries to access this\npage but they're not authenticated.", "start": 6197.74, "duration": 3.9 }, { "text": "How would we even find that out,\nand what do we do in that situation?", "start": 6201.64, "duration": 3.1 }, { "text": "Well, let's say, if not\nrequest.user.is_authenticated.", "start": 6204.74, "duration": 6.25 }, { "text": "The request object that gets\npassed in as part of the request", "start": 6210.99, "duration": 2.84 }, { "text": "to every user in Django\nautomatically has", "start": 6213.83, "duration": 2.27 }, { "text": "a user attribute associated with it.", "start": 6216.1, "duration": 2.22 }, { "text": "And that user object has an is\nauthenticated attribute that tells us", "start": 6218.32, "duration": 4.08 }, { "text": "if the user is signed in or not.", "start": 6222.4, "duration": 2.4 }, { "text": "If they're not signed in, we'll\ngo ahead and HTTP response", "start": 6224.8, "duration": 3.9 }, { "text": "redirect them to the log in view.", "start": 6228.7, "duration": 4.442 }, { "text": "And in order to make\nthis work, I'm going", "start": 6233.142, "duration": 1.708 }, { "text": "to need to-- from django.http\nimport HttpResponseRedirect.", "start": 6234.85, "duration": 5.46 }, { "text": "And likewise, from django.urls, let's\ngo ahead and import reverse as well.", "start": 6240.31, "duration": 6.16 }, { "text": "So if the user is not\nauthenticated, then", "start": 6246.47, "duration": 2.45 }, { "text": "we're going to redirect them to the log\nin view, where what is the log in view", "start": 6248.92, "duration": 3.63 }, { "text": "going to do?", "start": 6252.55, "duration": 1.42 }, { "text": "Well, the log in view for now,\nlet's just go ahead and render", "start": 6253.97, "duration": 4.97 }, { "text": "users/login.html.", "start": 6258.94, "duration": 2.73 }, { "text": "Some form where the user\ncan log themselves in.", "start": 6261.67, "duration": 3.11 }, { "text": "We'll need to create some templates.", "start": 6264.78, "duration": 1.57 }, { "text": "I'll create a templates folder\ninside of which is a user's folder.", "start": 6266.35, "duration": 4.14 }, { "text": "Inside of which we'll just\ncreate a basic layout,", "start": 6270.49, "duration": 2.4 }, { "text": "as we've done multiple times now.", "start": 6272.89, "duration": 3.31 }, { "text": "This is, again, going to be the general\nstructure for pages in this app.", "start": 6276.2, "duration": 3.81 }, { "text": "Title will be users,\nand the body will just", "start": 6280.01, "duration": 2.79 }, { "text": "have a block called body that I can\nlater fill in with other content.", "start": 6282.8, "duration": 5.61 }, { "text": "And now that I have this HTML layout,\nI can go ahead and create a new file", "start": 6288.41, "duration": 3.93 }, { "text": "called login.html, where login.html\nwill extend user/layout.html.", "start": 6292.34, "duration": 7.73 }, { "text": "And inside the body block, I\ncan just display in HTML form.", "start": 6300.07, "duration": 6.552 }, { "text": "So I can say something\nlike, I would like", "start": 6306.622, "duration": 1.708 }, { "text": "for there to be a form, whose\naction, when I submit the forum--", "start": 6308.33, "duration": 3.54 }, { "text": "let's go ahead and still\ngo to the log in URL,", "start": 6311.87, "duration": 3.0 }, { "text": "but let's do so using\nthe post request method.", "start": 6314.87, "duration": 3.09 }, { "text": "Again, I'm logging in,\nI'm submitting a form.", "start": 6317.96, "duration": 2.16 }, { "text": "Generally, when you're\ndoing that, you want", "start": 6320.12, "duration": 1.792 }, { "text": "to submit form data via post, especially\nin the case of user name and password.", "start": 6321.912, "duration": 3.458 }, { "text": "Because if you do this\nsort of thing, you", "start": 6325.37, "duration": 1.842 }, { "text": "don't want the user name and password\nto be passed in as get parameters", "start": 6327.212, "duration": 2.958 }, { "text": "because those show up in the URL.", "start": 6330.17, "duration": 2.43 }, { "text": "Our form will have our CSRF\ntoken for security as before.", "start": 6332.6, "duration": 3.93 }, { "text": "And input whose type is text,\nwhose name is username, and just", "start": 6336.53, "duration": 4.56 }, { "text": "for user friendliness, let's give\nit a placeholder also of user name", "start": 6341.09, "duration": 3.15 }, { "text": "so the user knows to type\nin their user name here.", "start": 6344.24, "duration": 3.12 }, { "text": "We'll also have an input whose type is\npassword, whose name is also password,", "start": 6347.36, "duration": 5.11 }, { "text": "and when an input's type\nis password, that just", "start": 6352.47, "duration": 2.15 }, { "text": "means our HTML will know in the browser\nthat chrome or Safari or whatnot", "start": 6354.62, "duration": 3.57 }, { "text": "will know to show the password\nas dots instead of as characters.", "start": 6358.19, "duration": 4.02 }, { "text": "And we'll give that a\nplaceholder of password.", "start": 6362.21, "duration": 3.48 }, { "text": "And then an input of type\nsubmit whose value is log in.", "start": 6365.69, "duration": 4.32 }, { "text": "So we now have the ability to log in.", "start": 6370.01, "duration": 3.39 }, { "text": "So if we go ahead and run this\nprogram, python manage.py run server,", "start": 6373.4, "duration": 3.96 }, { "text": "we should see users.views\nhas no attribute log in view.", "start": 6377.36, "duration": 4.44 }, { "text": "All right, it looks like I called\nthis function log in request.", "start": 6381.8, "duration": 3.45 }, { "text": "It should actually be\ncalled log in view.", "start": 6385.25, "duration": 1.89 }, { "text": "And I'll also need a\nfunction called log out view.", "start": 6387.14, "duration": 2.94 }, { "text": "But I haven't implemented that yet.", "start": 6390.08, "duration": 1.86 }, { "text": "So I'll just go ahead\nand say pass for now,", "start": 6391.94, "duration": 1.91 }, { "text": "but I'll come back to that later\nto implement the log out view.", "start": 6393.85, "duration": 3.43 }, { "text": "All right, so it looks like\nmy web server is running now.", "start": 6397.28, "duration": 2.52 }, { "text": "And before I actually\ngo to the login page,", "start": 6399.8, "duration": 2.16 }, { "text": "let me first go back to the admin page\nand actually just create some users.", "start": 6401.96, "duration": 3.84 }, { "text": "I can go to users and then\nadd, and let's add a user.", "start": 6405.8, "duration": 3.34 }, { "text": "The user name will be\nHarry, for example.", "start": 6409.14, "duration": 2.63 }, { "text": "And we'll go ahead and\ngive Harry a password.", "start": 6411.77, "duration": 2.697 }, { "text": "And we'll go ahead and\nsave and add another.", "start": 6414.467, "duration": 1.833 }, { "text": "Let's add maybe Ron as well.", "start": 6416.3, "duration": 3.56 }, { "text": "Go ahead and add him.", "start": 6419.86, "duration": 1.45 }, { "text": "We'll go ahead and save that.", "start": 6421.31, "duration": 1.262 }, { "text": "And these users, they can\nhave additional information", "start": 6422.572, "duration": 2.208 }, { "text": "associated with them.", "start": 6424.78, "duration": 0.875 }, { "text": "I can give Ron a name like Ron\nWeasley, RonWeasley@example.com", "start": 6425.655, "duration": 3.75 }, { "text": "is his email address.", "start": 6429.405, "duration": 1.345 }, { "text": "There are a bunch of default\nfields that Django gives you", "start": 6430.75, "duration": 2.67 }, { "text": "for manipulating users.", "start": 6433.42, "duration": 1.312 }, { "text": "And you can take these users and\ngo ahead and add to those fields", "start": 6434.732, "duration": 2.708 }, { "text": "if I want to.", "start": 6437.44, "duration": 0.57 }, { "text": "Giving them a first name, last\nname, email address, and whatnot.", "start": 6438.01, "duration": 3.68 }, { "text": "And you can also customize\nthese fields as well.", "start": 6441.69, "duration": 2.0 }, { "text": "If you'd like to add custom fields that\nyou would like to keep track of with", "start": 6443.69, "duration": 3.29 }, { "text": "regards to your individual users.", "start": 6446.98, "duration": 2.61 }, { "text": "And I will go ahead and\nlog out from Django admin", "start": 6449.59, "duration": 2.13 }, { "text": "now because I don't need it anymore.", "start": 6451.72, "duration": 2.16 }, { "text": "But now, if I go to /users,\nI'm not authenticated.", "start": 6453.88, "duration": 4.435 }, { "text": "So what I see is a log in form\nthat just looks like this.", "start": 6458.315, "duration": 2.375 }, { "text": "A place for me to type in\na user name and a password.", "start": 6460.69, "duration": 3.33 }, { "text": "And of course, I could type in\nHarry's username and password.", "start": 6464.02, "duration": 2.55 }, { "text": "But I haven't yet implemented\nthe processing of that data yet.", "start": 6466.57, "duration": 3.27 }, { "text": "So let's go ahead and do that now.", "start": 6469.84, "duration": 2.44 }, { "text": "We'll go ahead and go back to views.py.", "start": 6472.28, "duration": 2.99 }, { "text": "In the log in view, there are\ntwo ways the log in view function", "start": 6475.27, "duration": 2.663 }, { "text": "could be called.", "start": 6477.933, "duration": 0.667 }, { "text": "One is via the get request method,\nmeaning just show me the log in form.", "start": 6478.6, "duration": 3.66 }, { "text": "And one is via post, submit\ndata to the log in form as well.", "start": 6482.26, "duration": 3.58 }, { "text": "So if the request method\nis post, well then,", "start": 6485.84, "duration": 4.73 }, { "text": "let me first get the user name\nwhich will be inside of the post", "start": 6490.57, "duration": 3.06 }, { "text": "data in a field called user name.", "start": 6493.63, "duration": 2.1 }, { "text": "And let me get the\npassword, which will be", "start": 6495.73, "duration": 1.98 }, { "text": "in a field in the request.post\ninside of password.", "start": 6497.71, "duration": 5.0 }, { "text": "And now what I'd like to do is\ntry to authenticate this user.", "start": 6502.71, "duration": 3.94 }, { "text": "And how do I go about doing that?", "start": 6506.65, "duration": 1.52 }, { "text": "Well, it turns out there are a\ncouple of functions that Django", "start": 6508.17, "duration": 2.0 }, { "text": "has given to me that I can import.", "start": 6510.17, "duration": 1.76 }, { "text": "So from django.contrib.auth,\nauth for authentication.", "start": 6511.93, "duration": 4.27 }, { "text": "I'm going to import three functions\nwe're ultimately going to use.", "start": 6516.2, "duration": 2.79 }, { "text": "One is authenticate that checks if\nuser name and password are correct.", "start": 6518.99, "duration": 3.67 }, { "text": "One is called log in,\none is called log out.", "start": 6522.66, "duration": 4.75 }, { "text": "And I can now use those functions\ninside of this log in view here.", "start": 6527.41, "duration": 3.52 }, { "text": "After I've gotten the\nusername and password,", "start": 6530.93, "duration": 1.95 }, { "text": "I'd like to authenticate the user.", "start": 6532.88, "duration": 1.53 }, { "text": "Check if the user name\nand password are correct.", "start": 6534.41, "duration": 2.65 }, { "text": "So I'll go ahead and say user is equal\nto authenticate request username is", "start": 6537.06, "duration": 5.18 }, { "text": "the username password equals password.", "start": 6542.24, "duration": 3.04 }, { "text": "And so authenticate is a function.", "start": 6545.28, "duration": 1.55 }, { "text": "Just takes the request, takes\na user name, takes a password,", "start": 6546.83, "duration": 3.57 }, { "text": "and if the user name\nand password are valid,", "start": 6550.4, "duration": 2.13 }, { "text": "they give me back who\nthe user actually is.", "start": 6552.53, "duration": 3.61 }, { "text": "And as long as the\nuser is not none, that", "start": 6556.14, "duration": 4.31 }, { "text": "means the authentication was successful,\nand I can go ahead and log the user in.", "start": 6560.45, "duration": 4.11 }, { "text": "How do I log the user in?", "start": 6564.56, "duration": 1.5 }, { "text": "I use the log in function\nthe Django gives me.", "start": 6566.06, "duration": 2.43 }, { "text": "Logging in with this request, this user.", "start": 6568.49, "duration": 3.43 }, { "text": "And now I can go ahead\nand redirect them.", "start": 6571.92, "duration": 2.42 }, { "text": "HttpResponseRedirect the\nuser back to the index route.", "start": 6574.34, "duration": 4.2 }, { "text": "Back to the original route\nthat the user started out as.", "start": 6578.54, "duration": 2.767 }, { "text": "And so that's if the user is not none.", "start": 6581.307, "duration": 1.583 }, { "text": "If the authentication was successful.", "start": 6582.89, "duration": 2.04 }, { "text": "But otherwise, if the authentication\nfailed, what should I do?", "start": 6584.93, "duration": 3.69 }, { "text": "Well, let me go ahead and render\nthe same users login page again.", "start": 6588.62, "duration": 5.13 }, { "text": "But let me add some additional context.", "start": 6593.75, "duration": 2.52 }, { "text": "The context will be a message\nthat says invalid credentials.", "start": 6596.27, "duration": 6.13 }, { "text": "And now, inside of\nlogin.html, I can just", "start": 6602.4, "duration": 2.66 }, { "text": "add some logic that says,\nif there's a message,", "start": 6605.06, "duration": 3.39 }, { "text": "then go ahead and display\nthat message inside of a div.", "start": 6608.45, "duration": 4.17 }, { "text": "And then endif to end that.", "start": 6612.62, "duration": 1.47 }, { "text": "So if there is a message,\nwe'll see the message printed.", "start": 6614.09, "duration": 2.46 }, { "text": "Otherwise, we won't see it at all.", "start": 6616.55, "duration": 2.86 }, { "text": "So now, if I go ahead and\nrefresh the login page,", "start": 6619.41, "duration": 2.66 }, { "text": "nothing seems to have changed.", "start": 6622.07, "duration": 1.47 }, { "text": "But let's say I type in a\nuser name that doesn't exist.", "start": 6623.54, "duration": 2.36 }, { "text": "Hermione and some password and I log in.", "start": 6625.9, "duration": 2.98 }, { "text": "Well, then I get this error message.", "start": 6628.88, "duration": 1.5 }, { "text": "Invalid credentials.", "start": 6630.38, "duration": 1.11 }, { "text": "We were not able to log the user in.", "start": 6631.49, "duration": 2.35 }, { "text": "So what happens if we do\nsuccessfully logged in?", "start": 6633.84, "duration": 2.45 }, { "text": "Well, then the user is going to\nbe taken to this index route.", "start": 6636.29, "duration": 4.178 }, { "text": "And it looks like now we need\nto finish off this index route.", "start": 6640.468, "duration": 2.542 }, { "text": "What does the index route do?", "start": 6643.01, "duration": 1.53 }, { "text": "Well, let's go ahead and return render\na template called users/user.html.", "start": 6644.54, "duration": 4.62 }, { "text": "And inside of user.html,\nwe'll go ahead and display", "start": 6652.28, "duration": 4.7 }, { "text": "some information about the user.", "start": 6656.98, "duration": 2.55 }, { "text": "We'll still extend\nuser/layout because we're", "start": 6659.53, "duration": 2.7 }, { "text": "going to use the same basic layout.", "start": 6662.23, "duration": 2.13 }, { "text": "But in the body of this page,\nthe information I want to show--", "start": 6664.36, "duration": 3.86 }, { "text": "if I want to say welcome and then\nlike Harry or welcome Ron or whoever", "start": 6668.22, "duration": 4.84 }, { "text": "the user happens to be.", "start": 6673.06, "duration": 1.99 }, { "text": "And it turns out inside\nof Django templates,", "start": 6675.05, "duration": 2.39 }, { "text": "I have access to the request that was\nused to make this HTTP request, which", "start": 6677.44, "duration": 4.44 }, { "text": "means I also have access to\nrequest.user who is the user associated", "start": 6681.88, "duration": 4.41 }, { "text": "with that request.", "start": 6686.29, "duration": 1.29 }, { "text": "And if the user has a first name, I\ncan access request.user.firstname.", "start": 6687.58, "duration": 5.965 }, { "text": "And in addition to that, I\ncan display other information.", "start": 6693.545, "duration": 2.375 }, { "text": "Maybe their user name is\nrequest.user.username.", "start": 6695.92, "duration": 3.78 }, { "text": "And maybe their email address\nis request.user.email.", "start": 6699.7, "duration": 4.23 }, { "text": "And so I can show the user\ninformation about them.", "start": 6703.93, "duration": 3.72 }, { "text": "Such that if Harry logs in, for\nexample, I sign in as Harry,", "start": 6707.65, "duration": 3.84 }, { "text": "sign in with Harry's\ncredentials, click log in.", "start": 6711.49, "duration": 4.56 }, { "text": "Well then, Harry sees a page\nthat says welcome Harry.", "start": 6716.05, "duration": 2.91 }, { "text": "Harry is logged in.", "start": 6718.96, "duration": 1.08 }, { "text": "They are request.user.", "start": 6720.04, "duration": 1.98 }, { "text": "And using that information, we can\naccess first name, username, and email", "start": 6722.02, "duration": 5.13 }, { "text": "as well just by accessing\nproperties of request.user.", "start": 6727.15, "duration": 4.8 }, { "text": "Now, last thing we need to add,\nwhich still doesn't yet exist,", "start": 6731.95, "duration": 3.06 }, { "text": "is a way to actually log the user out.", "start": 6735.01, "duration": 2.5 }, { "text": "And it turns out that just as\nDjango has a log in function,", "start": 6737.51, "duration": 3.86 }, { "text": "Django also has a log out function\nthat handles log out for us so we", "start": 6741.37, "duration": 3.24 }, { "text": "don't need to implement it ourselves.", "start": 6744.61, "duration": 1.83 }, { "text": "So all our log out view needs to do is\nmake a call to this log out function.", "start": 6746.44, "duration": 5.25 }, { "text": "And then, figure out where should the\nuser go after they've been logged out.", "start": 6751.69, "duration": 3.21 }, { "text": "And you know what, let's go ahead\nand take them back to the login page", "start": 6754.9, "duration": 5.5 }, { "text": "with a message of logged out to indicate\nthat the user has now been logged out.", "start": 6760.4, "duration": 6.0 }, { "text": "Then in user.html, we'll go ahead and\nadd a link that will go to the log", "start": 6766.4, "duration": 7.89 }, { "text": "out route that just says,\nlog out, for example.", "start": 6774.29, "duration": 5.61 }, { "text": "So now, when Harry goes\nback to Harry's page,", "start": 6779.9, "duration": 2.06 }, { "text": "Harry sees a URL that says log out.", "start": 6781.96, "duration": 2.19 }, { "text": "If Harry clicks log out, Harry gets\nlogged out is brought back to this page", "start": 6784.15, "duration": 4.23 }, { "text": "because now request.user.isauthenticated\nis going to be false.", "start": 6788.38, "duration": 4.15 }, { "text": "There is no authenticated user.", "start": 6792.53, "duration": 1.88 }, { "text": "And so they now see just\nthe default login page.", "start": 6794.41, "duration": 3.06 }, { "text": "And if now Ron were to log in,\nfor example, using Ron's username", "start": 6797.47, "duration": 3.84 }, { "text": "and Ron's password\nlogging in, then Ron now", "start": 6801.31, "duration": 3.39 }, { "text": "sees information associated\nwith him as well.", "start": 6804.7, "duration": 3.27 }, { "text": "So Django gives us a lot out of the box.", "start": 6807.97, "duration": 2.04 }, { "text": "Gives us the ability to represent\nthese models in admin interface,", "start": 6810.01, "duration": 3.18 }, { "text": "to be able to manipulate them.", "start": 6813.19, "duration": 1.35 }, { "text": "A migration system that allows us to\nvery quickly make changes to our models", "start": 6814.54, "duration": 3.93 }, { "text": "and apply them to our database.", "start": 6818.47, "duration": 1.59 }, { "text": "And also, a built in user\nauthentication system.", "start": 6820.06, "duration": 2.46 }, { "text": "A system that allows us\nto very quickly enable", "start": 6822.52, "duration": 2.61 }, { "text": "users to be able to log in, log out,\nfrom our web application as well.", "start": 6825.13, "duration": 4.268 }, { "text": "So all of this are\nfeatures that just helps", "start": 6829.398, "duration": 1.792 }, { "text": "to make it so that we can very quickly\ntake advantage of things like SQL", "start": 6831.19, "duration": 3.57 }, { "text": "and models and migrations to\nbuild dynamic, interesting web", "start": 6834.76, "duration": 3.18 }, { "text": "applications with data to back them up.", "start": 6837.94, "duration": 2.67 }, { "text": "This was web programming\nwith Python and JavaScript.", "start": 6840.61, "duration": 2.28 }, { "text": "We will see you next time.", "start": 6842.89, "duration": 2.27 } ]