text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
So what I'd like to do is somehow tell Django
3,095.53
3.06
that you should update the database to include information about the models
3,098.59
4.17
that I have just created.
3,102.76
1.5
This is a process that we refer to in Django and more generally as
3,104.26
3.15
migrations.
3,107.41
1.24
I create a migration to say, here are some changes
3,108.65
3.05
that I would like to apply to the database.
3,111.7
2.37
Then I migrate them to tell Django to take those changes
3,114.07
3.6
and actually apply them to the database.
3,117.67
2.18
So it's a 2-step process.
3,119.85
1.33
One is creating the migration, the instructions for how to actually go
3,121.18
3.93
about manipulating the database.
3,125.11
1.5
And then one to take that migration step of saying now take those instructions
3,126.61
4.29
and actually apply them to the underlying database.
3,130.9
3.9
We can make the migrations via command.
3,134.8
2.52
Again we'll use the manage.py script that has a number of different commands
3,137.32
3.66
that allow us to control various parts of the application.
3,140.98
2.85
I'll use python manage .py and then MAKE migrations.
3,143.83
5.46
Now what we see is we've created a migration inside of 0001_initial.py
3,149.29
6.3
where in this migration it's created a model called "flight."
3,155.59
5.06
So if I go ahead and look at the migrations directory,
3,160.65
3.22
I see this file has been created for me.
3,163.87
2.89
I didn't have to create it myself.
3,166.76
1.85
This file has instructions to Django for how
3,168.61
3.72
to manipulate the database to reflect the changes I have made to the model.
3,172.33
4.47
Here is an instruction to Django to create a new model called
3,176.8
3.75
"flight" that has these particular fields inside of it.
3,180.55
3.75
It's basing this off of the changes that I made to models.py.
3,184.3
3.39
The model that I added is now reflected in this migration.
3,187.69
4.59
Now if I want to apply the migration, actually apply it to Django's database,
3,192.28
4.47
I can run python manage.py MIGRATE to go ahead and apply these migrations.
3,196.75
5.542
There are a bunch of default migrations that get applied as well,
3,202.292
2.708
but notice that one of the migrations that gets applied is this one here.
3,205
3.87
Applying flights.0001_initial to say let's go ahead and apply that
3,208.87
5.1
migration, create that table that is going to represent flights.
3,213.97
4.53
If I type LS now, you'll see that now I have a db.sqlite3 file.
3,218.5
5.37
A SQLite database that is going to contain a table that
3,223.87
3.72
is going to store all of my flights.
3,227.59
2.827
And so how can I actually begin to manipulate this data?
3,230.417
2.333
How can I interact with these sorts of models?
3,232.75
3.72
I could use direct SQL syntax by opening up this database file
3,236.47
3.51
and running commands, but Django provides some nice abstraction layers
3,239.98
3.51
on top of it so that I don't actually need to execute those commands myself.
3,243.49
3.63
I can begin to work more generally with Python classes and variables and things
3,247.12
4.41
that I'm used to inside the Python language.
3,251.53
2.88
So I can enter Django's shell where I can just run Python commands
3,254.41
3.757
by running python manage.py shell.
3,258.167
1.417
What this does is open up a shell or a console
3,262.75
1.92
where I can begin to write Python commands that
3,264.67
3.03
get executed on this web application.
3,267.7
2.67
The first thing I'd like to do is from flights.models
3,270.37
3.99
let me just import flight.
3,274.36
2.66
So "flights" is the name of my app.
3,277.02
1.48
"Models" is the name of that file.
3,278.5
1.74
I'm importing the flight class from that models file that I've just created.
3,280.24
4.53
Now what I can do is I can create a new flight.
3,284.77
3.54
I can say something like f= a flight whose origin is "New York"
3,288.31
5.88
and whose destination is "London" and whose duration= 415 minutes.
3,294.19
6.63
And then I can say f.save to save that new flight that I have created.
3,300.82
4.442
This syntax-- I'll go ahead and make it a little bit bigger so you can see it
3,305.262
3.208
a little easier--
3,308.47
0.99
is my way of inserting data into this table.
3,309.46
3.67
I don't need to use an INSERT query in SQL.
3,313.13
2.39
I just have to write a Python command, and Django knows that when
3,315.52
3.09
I create a new flight and save it, that it should run an instant command
3,318.61
4.14
on the underlying SQL tables.
3,322.75
1.59
Where here I've created a new flight with this particular origin
3,324.34
3.57
and destination and duration and I've gone ahead and saved that flight as
3,327.91
4.11
well.
3,332.02
1.04
If I want to query that flight, get information about that flight,
3,333.06
3.1
I can say something like, flight.objects.all
3,336.16
4.08
is the equivalent of a Select-All.
3,340.24
1.77
Get me all of the flights that exist inside of my database.
3,342.01
4.38
Here I see I get back a query set, which is just a set of results.
3,346.39
4.35
Here I have one flight that came back, flight object 1.
3,350.74
3.33
So a flight has been created for me with ID 1.
3,354.07
2.76
Now "flight object 1" probably not all that helpful of a name.
3,356.83
3.12
It'd be nicer if this model had a cleaner
3,359.95
2.16
way of seeing the name of a particular flight, for example.
3,362.11
4.23
And it turns out we can do that.
3,366.34
1.8
Any model-- I'll go back to the code inside of models.py--
3,368.14
3.54
any model can implement double underscore str
3,371.68
3.84
function, which returns a string representation
3,375.52
4.2
of that particular object.
3,379.72
1.89
This applies not just to Django models but to Python classes more generally.
3,381.61
3.48
That if this function returns a string representation of the object,
3,385.09
3.31
let's go ahead and return a formatted string that is self.id
3,388.4
4.7
I'll say self.origin to self.destination.
3,393.1
4.32
So here what I've said is that the string representation of any flight
3,397.42
4.11
is going to be a string that gives its ID and then says origin to destination.
3,401.53
4.5
Just a nice clean name that is going to represent this particular flight.
3,406.03
4.77
So now if I go back to the shell by running python manage.py shell,
3,410.8
6.35
I can say from flights.models import Flight.
3,417.15
3.508
I can say, all right, let's let a variable called
3,420.658
2.042
flights be equal to flight.objects.all.
3,422.7
3.66
And now flights is going to be this flight, flight 1, New York to London.
3,426.36
5.4
It now has a much nicer, string representation
3,431.76
2.22
of the name, which just makes it a little bit easier to interact with.
3,433.98
3.69
If I wanted to get just that one flight, I can say flight equals flights.first.
3,437.67
5.34
Flights is a query set.
3,443.01
1.47