In this lesson, you’re going to start looking at databases and Django models. When you created your app before, you created a file called
models.py. The models are where you do the database interactions.
This is what a Django model looks like:
# Create your models here. class Project(models.Model): title = models.CharField(max_length=100) description = models.TextField() technology = models.CharField(max_length=20) image = models.FilePathField(path='/img/')
00:43 So, the first thing I want to talk about is the Django ORM. What’s an ORM? It’s an object-relational mapper. What this word already has, in itself, is it tells us that it makes a connection between objects and relational tables.
00:58 So essentially, the ORM gives us a way to interact between Python and SQL—SQL as a relational database management system. We have these objects in Python that we’re creating, that are going to be translated to tables, more or less, in SQL—and the other way around.
01:18 Okay, so the ORM allows us this communication between these two different ways of representing data. Now, you might be wondering, “What’s a relational database?” You’ve probably heard of SQL before. SQL stands for Structured Query Language, and that’s a way of interacting with relational databases.
01:35 If you’re wondering what a relational database is, I have a very, very short intro to what this is. I hope it’s going to look familiar. One of the basic concepts about it are tables and tables interacting with each other. You’ve probably seen a table before if you use Google Sheets, or if you use Excel. It’s a common way of structuring data.
02:15 That’s the way that you can essentially access every piece of information inside of the table. It’s just a way of storing data. Now, let’s look back at this Python code that we saw for a moment before. This is our rendering of a model class in Python.
And those correspond to the column names, with one exception. You can see that
id isn’t here, and that’s simply because Django does that automatically for us.
id, usually, is just an auto-incrementing number, and Django just takes away from us the need to create this. Okay, so inside of our model class, we’re defining the different columns.
And what we’re doing here on the right side is we’re telling it what type of data can be inside of this column. So, looking back to this, if we look at the column
image, we’re telling it, “Okay, this type of data,” in our case, it’s going to be a
FilePathField. See right here? “That’s what’s allowed to be in here, nothing else.” So,
id is going to be an auto-incrementing integer. Our table, here, isn’t that diverse.
20, while the
TextField is another way of strings, characters, but we’re leaving it open, so it’s open-ended. Inside of our database representation, you’re often going to see something like that, but here, this is the model of the database on the more SQL-side of things, right?
So, we see the
id is going to be of data type integer. Again, that gets created automatically. And then we see here,
title has this data type here of varchar(100), and that’s the SQL representation of what we’re making here. Again, this ORM allows us to translate between these two different languages,
04:13 but you can inspect the data and you can see that this is what it results into. Eventually, it’s going to result into this: a table that looks very familiar to the table that we looked at before.
04:24 It has columns that are named the same as how we named the columns in here. And then later, when we’re going to put some data into this, the rows are going to be filled up with these different textual items, this data.
Let’s take a look, actually: how does this look like in our app? If you remember, this is what it results into, okay? So, we’re going to have the
image path that results into displaying the image, we’re going to have the
title that shows us the title here.
So, that’s essentially what I want you to think about when you look at this code over here that we’re writing. Django
Model, all it does is it makes it easier for us, makes it possible for us to write Python code that then translates into a table inside of a relational database.
Model class. That just gives us a lot of functionality that is going to be the same, and it’s going to allow us to interact and makes it possible for us to actually just write this very simple code, but still get a functional model. So, we want a
title. Remember, we don’t need to create the
id. Django does that for us.
And we said that’s going to be a
CharField (character field), with a
100. I’m just defining this so that people don’t put in endless titles, and it saves with performance.
title, then we have a
06:32 There’s lots and lots of these fields that are possible. You can head over to the Django documentation, which has an excellent reference to all the possible fields that you can use. But most of those, you’re probably not going to need in a normal app that you’re building.
So, just know that there’s references for this, you can look them up, you don’t need to know them. The ones that you’re going to be using very often, they become familiar pretty soon. Then, we have
FilePathField, which just tells us that it’s essentially going to be a string, but a string that has certain restrictions. Okay, I’m defining this to be
'/img' (image). Now, here we are. That’s our model.
07:35 I hope you can see, now, the relation to how these ones are going to create the columns, and this is telling us what is allowed to be inside of the rows related to those columns, and how that—again—relates back to what are we actually going to see popping up in our app when we have it finished and when we have the database filled up with some data.
Become a Member to join the conversation.