Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What's the ORM used for again? And what methods have we used? If you have these questions in mind, we can answer them!
[MUSIC]
0:00
Hi, I'm Kenneth,
the Python teacher here at Treehouse.
0:04
We've been using Django's ORM in
all of our agenda courses so far.
0:07
We used it to get single records out
that matched a certain primary key.
0:10
And we used it to get
all of the records for
0:14
a certain model like when we
made our list of courses.
0:16
We also used it when we were making model
formsets and needed a set of blank forms.
0:19
Let's do a mini review.
0:24
We'll use our course model.
0:25
If we want all of the courses
that are saved in the database,
0:27
we would use course.objects.all.
0:30
If we didn't want any of them but
we wanted a query set,
0:32
we'd use course.objects.none.
0:35
And if we needed just a single course,
0:37
we would use the get method to
pull it out using a comparison.
0:40
Of course,
if we think that course might not exist,
0:44
we'd use the handy get object or
404 function.
0:47
The last thing we used the ORM for
was creating and saving records.
0:51
We used the create method to make a new
instance and the save method to save
0:54
changes to an existing one or to insert
an in-memory instance into the database.
0:58
But even with all of that,
1:03
there's still a lot about the ORM
that we haven't looked at.
1:04
I mean, heck, I haven't even given you a
formal definition for what a query set is.
1:07
A query set, at least in Django's terms,
1:11
is a collection of records
returned from the database.
1:13
You might be expecting the collection
to be a set since that's in the name or
1:15
at least unique but
that's not always the case.
1:19
Anything that could come back from a plain
old SQL query can be in a query set.
1:22
Query sets are also lazy.
1:27
Lazy is a word you'll hear
a lot in Django, actually.
1:29
When we use lazy in a programming concept,
we usually mean that whatever work
1:32
the lazy thing is going to do won't
be done until we use the lazy thing.
1:36
For instance,
query sets exist only in memory.
1:41
They don't hit the database at all until
you consume them with Lynn, a slice,
1:43
iteration or some other options.
1:48
I'll include a link to more information
about this in the teacher's notes.
1:51
Most of this course will be dealing
with the model layer of Django.
1:54
Things like, well, models and query sets.
1:58
So in the next video, let's check out
the current state of our models, our data,
2:00
and make a couple of small changes.
2:04
You need to sign up for Treehouse in order to download course files.
Sign up