Django ORM: Accessing a Single Project
To get started, go into the Django shell and bring up
models.py so you can have an overview of what’s in that file. First, you need to import:
>>> from projects.models import Project
Now, you can interact with it and revisit what you did to get all of the models:
>>> projects = Project.objects.all()
and now we’re inside of the Django shell. I’m just going to pull up here also the
models file, because we’re going to deal with our models, so it’s nice to have an overview sitting up here. Cool.
and I see that
projects.models, so our
Project model in here, let’s go ahead and say
from projects.models import Project, and now we can interact with it. What I’m going to do next, I’m going to revisit what we did to get all of the models. So we did, like we said,
projects = Project…
.all(), and that’s returned to us a
QuerySet that contains all of the projects that are currently in the database. Cool. What I did to get to one
Project was I said,
p1 is—from this selection, I can slice it and I can say, “Give me the first item,”
so now I have access to this one
Project object. I can go to its
.title, et cetera. But that’s not the most convenient way of doing it because how am I going to know how many projects are in there?
How am I going to access this very specific one? Slicing here is not the best way to do this. And the Django ORM has its specific function for doing just that: getting a specific
Project. And the function looks like that.
So what I can do in here is I can say
id=2, so this is going to be this one here. I get access to my second
Project object. Again, I can do things like that—
.title—just to prove that now we’re at the second one. Cool!
So, maybe what you might see online or what we’re actually also going to use in this is not
id—that’s the name of our column here—but there is something called a primary key. Instead of
id, what his
id column stands for is actually the primary key of our table, so what you can type instead of
pk, and this has the same effect.
Let’s take a bit of a closer look at this. What is
pk stands for primary key. If you remember, when we took a look at our table that we created—the
projects table—in here, we have
technology, which are column names that we defined in our Django model.
Then, there’s this automatically-generated one that Django names
id is the name of it, and in our project, the primary key, therefore, is the same as
id. You could name this differently, you could choose another column to be the primary identifier, but every table needs to have a primary key.
So keep in mind, if you see
pk, it stands for the primary key and that’s our unique identifier for one object inside of our table. And that’s exactly what we’re looking for, so we want to target one row in our table and pick all that information from that row out, put it back into a Python object, and then be able to use it in our code.
.objects.get—the name of our model
.objects.get, and then we’re passing in a primary key. In this case, that’s an
id, which is an auto-incrementing number. I’m selecting a specific one—in this case, our second
Project. All right, that’s all for now. In the next video, we’re going to head over and use this newly-found knowledge of accessing one specific
Project by its primary key in order to build out this page where we can just see this one specific object.
Become a Member to join the conversation.