text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
First gets me that first flight.
3,444.48
2.44
And so now, I have this flight from New York to London.
3,446.92
3.5
And just as in any Python object, I can begin
3,450.42
2.13
to access properties of that object.
3,452.55
2.18
I can say, all right, flight, what is your ID?
3,454.73
2.26
Flight, what is your origin?
3,456.99
1.77
Flight, what is your destination?
3,458.76
1.44
Flight, what is your duration?
3,460.2
1.63
And I can access, as values, all of the properties of this flight
3,461.83
3.54
that I ultimately care about.
3,465.37
1.22
And if I want to delete the flight, I can say something like flight.delete.
3,466.59
5.13
Now, ultimately though, this is not the model
3,471.72
2.79
that I actually want to represent my flight.
3,474.51
2.1
Because here again, I'm using a character field, a char field,
3,476.61
3.48
for things like origin and destination.
3,480.09
1.92
When in reality, I'd probably like to use something like another table
3,482.01
4.38
for representing airports, and then some relationship
3,486.39
3.36
between every flight and an airport.
3,489.75
2.34
So let's go ahead and try and implement that idea now,
3,492.09
2.46
that I can go back into models.py and create a new class.
3,494.55
4.29
I'll create a class called airport.
3,498.84
2.4
That is also a model.
3,501.24
1.77
And I'd like for this airport class to have
3,503.01
2.52
a code, which is a character field with a max length of 3,
3,505.53
5.43
for the airports code.
3,510.96
1.47
As well as a city, which would be a character field
3,512.43
2.49
with a max length of 64.
3,514.92
2.79
And let's also give this airport a string representation.
3,517.71
3.79
We'll say that the string representation of an airport
3,521.5
2.39
will just be the city of the airport, and then in parentheses,
3,523.89
4.29
the code of the airport.
3,528.18
1.1
So it will be something like New York, and then in parentheses
3,529.28
2.583
JFK to represent a particular airport.
3,531.863
4.147
And now, our flight model needs to change a little bit.
3,536.01
3.27
No longer will origin and destination be character fields
3,539.28
3.27
that are just storing text.
3,542.55
1.53
But instead, origin is going to be a foreign key.
3,544.08
4.83
A foreign key that references another table, like the airport table.
3,548.91
5.157
And then, I can provide some additional arguments.
3,554.067
2.083
So this alone would be enough.
3,556.15
1.35
But I can add some additional arguments like on delete equals models.cascade.
3,557.5
5.57
So what does this mean?
3,563.07
1.21
Well, when I have tables that are related to each other,
3,564.28
3.052
SQL needs some way of knowing what should
3,567.332
1.708
happen if you ever delete something.
3,569.04
1.83
If I have a flight from JFK to London, and later in time
3,570.87
5.07
decide to delete JFK airport from my database,
3,575.94
2.79
what should happen to that flight?
3,578.73
1.65
What happens to flights when the thing that it is referencing gets deleted?
3,580.38
4.44
What models.cascade means is if I were to ever delete
3,584.82
2.94
an airport from the airports table, it's going to also delete
3,587.76
3.54
any of the corresponding flights.
3,591.3
1.737
And there are other on delete parameters you
3,593.037
1.833
can set for saying like, don't even let me delete an airport
3,594.87
2.57
if there are flights that are leaving from
3,597.44
1.75
or going to that airport, that's called models.protect.
3,599.19
3.06
But there are other ways of implementing similar types of constraints.
3,602.25
4.41
And the other argument that I'm going to provide
3,606.66
2.04
is what's called a related name.
3,608.7
2.49
And a related name, as we'll see in just a moment,
3,611.19
2.34
is going to be a way of me accessing a relationship in the reverse order.
3,613.53
4.47
That from a flight, I can take a flight and say .origin to get
3,618
4.17
the flight's origin in airport.
3,622.17
1.648
But the other question I might want to ask is in the reverse order.
3,623.818
2.792
If I have an airport, how do I get all of the flights that
3,626.61
3.6
have that airport as an origin?
3,630.21
1.83
And so here, if I give a related name to this foreign key,
3,632.04
3.24
Django will automatically set up the relationship
3,635.28
2.73
going in that opposite direction.
3,638.01
1.99
And so here, well, if we have an airport,
3,640
2.39
and I want to know all of the flights that have that airport as their origin,
3,642.39
3.36
the reasonable name for a related name here is something like departures.
3,645.75
4.68
So if I have an airport, I can access all
3,650.43
2.61
of the departures, which gets me all of the flights that
3,653.04
2.91
are leaving from that airport.
3,655.95
2.4
And I'll likewise do the same thing here for destination.
3,658.35
2.61
Instead of a character field it's going to be a foreign key.
3,660.96
3.12
It's going to reference airport.
3,664.08
1.92
When we delete it, we'll go ahead and cascade it.
3,666
2.31
And the related name will be arrivals.
3,668.31
2.85
Because if I have an airport, I might want
3,671.16
1.89
to access all of the arrivals, all of the flights
3,673.05
2.76
that correspond to flights that are arriving
3,675.81
2.91
at that particular destination.
3,678.72
2.675
And so now, I've done two things.
3,681.395
1.375
I've added a new class called airport, and I've
3,682.77
2.85
modified my existing flight model.
3,685.62
2.76
So this has changed in my Python code, but it hasn't yet
3,688.38
2.73
changed in my database.
3,691.11
1.78
So in order to make the change in the database, again, it's a 2-step process.
3,692.89
3.78
Step one, python manage.py, make migrations,
3,696.67
3.83
to say look for any new changes that have been made to models.py,
3,700.5
4.87
and go ahead and create a migration instruction
3,705.37
2.96
for how to make those changes to the database.
3,708.33
2.62
And here, we see that we've created a new migration file.
3,710.95
4.49
And this migration is going to create a model called airport.
3,715.44
3.09
And it's also going to alter the destination field
3,718.53
2.85
and alter the origin field on my flight model.
3,721.38
2.91
Because as we know, we've changed destination and origin
3,724.29
3.06
to no longer be character fields, but to instead
3,727.35
2.43