Django Shell – Real Python
Your next step is to put data in your database. For this, you’re going to use the Django shell to put one project in your database. Head over to your terminal and start the Django shell by typing the following:
This opens up a terminal, but it has the Django settings already imported, so it allows you to work directly from the root folder of a Django project.
00:00
Next, we want to put some data into our database. For this, we’re going to use the Django shell. There’s some different ways of doing that, but I want to show you this very handy tool. We’re going to get started and put one Project into our database.
00:15
For this, I head over to my terminal, in here. I’m going to start the Django shell by typing python manage.py—as so often—and then shell. This opens up a terminal.
00:30
However, it has the Django settings already imported, so it allows you to work directly from the root folder of a Django project. So, if you want to do something within Django or with the database, then start your shell like this. Now, what I want to do is I want to create a Project object and put this one into the database. For this, I’m going to use Django’s ORM. So, here is our cheatsheet.
00:57
That’s what a Project instance is going to look like. It’s going to have a title, description, technology, and image—an image path.
01:05
We put the images already in here. So, let’s go ahead and just create one. What we need to be able to use, then, is we have to import this Project model.
01:16
We can find this inside of—let me close this for a moment—projects/ and then models.py. Okay, so this is where it sits. I’m going to say from projects.models import Project, and now I can create a Project.
01:37
So, my first project is going to be a Project instance that has a title.
01:45
It’s going to be "test project".
01:50
It’s going to have a description…
02:03
Yada, yada, I’m just going to fill this out. Make sure that you put in the right data types. Like, it’s clear that we’re going to use text for a CharField (character field), TextField, and you can’t go over the max_length or it’s going to complain, et cetera.
02:28
And for image, I’m just going to put in
02:32
the name of the image file as the rest of the path—you remember Django knows about static/ folders in all apps, and we told it about projects/ and img/, so we directed it from here to img/.
02:45
All I need to put in here is, really, the name of the image file. So, I’m going to say "testproject.png". Okay. This is my first Project instance. Let’s create it.
03:02
Cool. So, all done? Not quite. What we need to do first—similar to the migrations—there’s always a step in between before you actually commit something to the database. So what I need to do to put it into the database is I have to say p1.save().
03:22
And now, I have this one instance as a row sitting inside of my database. We can inspect this with this visual tool that I showed you before, go ahead and do that. I’m going to show you a different way of doing it, which is I can query here. So, I can say results = Project.objects.all().
03:43
With this, I’m retrieving all the Project objects from the database, and now I can look at this. It’s telling us it’s a QuerySet instance, as a result, and it contains one Project object.
03:58
Awesome! I can say, “Give me that first one.” I can say results[0] is going to be my p1—let’s call it different, just to—p, for this one.
04:13
I can say p.title, for example, and this is 'test project'. It’s what I just put in there—so, what I did. And that’s where we can see the power of using an object-relational mapper. Using Python, I created an object, then I simply said .save() to put it into my relational database, and here I’m—again—using the ORM to query the database, get some results back, and then you can see, I can access them again very similar—or, in the same way that I would access a Python object.
04:47
So that’s the ORM at work, and yeah, we put a Project inside of our database. So, let’s make sure to set up the rest that we need. We still need routes, we need views, and we need templates. Let’s make sure to set those up so that we can actually see this Project, also on the front end.