From Static to Dynamic Content
Hey, what's happening guys? Welcome to your sixth Django tutorial, and in this video, I'm going to introduce you to Django models.
Alright then. So far we've been messing around with URLs and views, and we've been sending back templates to the end user. However, at the minute, these are just static templates, but what we really want to do is start working with data. Because after all, a blog is a collection of articles, which is data, and that's going to be stored in a database, right? So we want our views and our templates to be a little bit more dynamic and not just send back static HTML, or send back maybe articles which are stored in a database table. So to do that, we're going to have to understand the concept of models.
Understanding Django Models
So what is a model then? Well, basically a model in Django is a class which represents a single table in a database. And each type of data that we have in our application, for example, that could be articles or users or authors or books or something else that we have in our application, each type of data is represented by its own model. Okay? So each model then maps to a single table in a database. So for example, if we had four models: articles, users, authors, and books, then we would have four tables in a database. Each table is mapped by a single model.
So it looks something like this. In the code, to represent a model, we have a class and we give that class a name, for example, Article. So this class then represents this table in a database. So don't worry too much about this code for now here, we're going to go over that in a second. But for example, this article has a title field and a body field, right? Generally in a blog. So that is stored in our model in the code, and that then is mapped to a database table called articles, which is going to store instances of this model. So we have three instances of the model here, and we have a generated ID for each one. Even though there's not an ID over here, there will be an ID in the table which is automatically generated. We have the title, which is a character field, and also the body, which is a text field. So each row here represents a single record inside this articles table, and this model is a way for us to map to the table.
Okay, so models in the code are going to allow us to create new instances of records and then store them or save them in the database table. And we can also use the Django ORM to interact with the database as well using these models. And we'll learn more about the ORM later on. For now, let's just create a model inside the code.
Creating the Article Model Class
Okay then, so back in our code, what I'd like to do is create a model for our articles. So inside this articles app right here, we have a models.py file that is automatically generated when we created this articles app. So we're going to store our model for our article inside here. So it's already imported this thing, models, which is built into Django, and we're going to use that in a second. So the first thing we need to do is create a class. Remember, a model is represented by a class in Django. And we're going to give this class a name, it'll be called Article, and I've given this a capital A. That is a convention when we're creating models, we start with a capital.
Okay, so this class is going to inherit from models.Model. Alright, so this is inbuilt into Django as well, and it's just inheriting some basic functionality that all models will have. So inside this class, what we need to do is define our different fields. What is going to be stored in an article? Well, let's think about it. We're going to have a title, a body, which is the whole text inside the article. We might have a date of when the article was published, and I think also a thumbnail or an image of some description, maybe the author who's writing it, and a slug, which is the URL address, if you like, of the article. So let's create some of these now. First of all, I'm going to say title. Then underneath, I'm going to say slug. Then I'll say body, and then I'll say date.
And we're also going to add another couple of fields later on, but we'll leave them for now. So I'll just add in a little comment here to say what we're going to add in later on. We'll say add in thumbnail later and also add in the author later, so who wrote this article. So now we have these four properties right here. We need to set them equal to something. What type of data is this?
Defining Model Fields and Types
Well, this title might be a text field or a character field. So a character field in Django is a small amount of text, and a text field is a large amount of text. So it makes sense that this is a small amount of text because a title is not a big paragraph, it's just a one-liner.
So while we're talking about fields, I just want to show you the documentation on these different field types, and I'll leave the link to this down below because they have a big list of all the different field types that we can have in a model. There's things for images, file uploads, text, dates, that kind of thing. So what we're doing is we're telling Django what type of data is going to be stored in each one of these different fields, because some of them will be text, some of them will be a date, some might be a file, an image, that kind of thing. Okay? So we need to tell Django here, okay, what type of data is a title? What type of data is a slug, a body, and a date? And we can find that out by reading through these different field types. So if we scroll down here, we'll see this CharField which I was just talking about, and this is for small to large sized strings. So that's what we're going to do to begin with. We're going to use that for the title.
So we're going to set the title equal to models because the different field types are on this thing right here. It's models.CharField, and we can also pass through in here different arguments. For example, if I want to set a max length of 100 characters, I can pass that through, and you can see the different things that we can pass through down here as well. You can see max_length we can use. Alright, so let's pass in max_length, set that equal to 100, meaning we don't want more than 100 characters for this title. Alright.
So the slug is going to be a field called a SlugField. That's built into Django as well. So I'll say models.SlugField. Okay, we don't need to pass any kind of extra argument in there. The body is going to be a TextField. So I'll say models.TextField. And you can see that TextField down here, if we scroll down enough, this is in alphabetical order, so we have to go down a fair whack. We can see right here, TextField. So the default widget for this is a textarea. So that's another thing. When we're telling Django here what kind of data each field is, when it comes to outputting a form in both the admin area or in the front-end for users to add a new article, it's going to know what type of widget, whether that's going to be an input field, a drop-down box, a file upload, or a text field. It's going to know what type to output depending on what we put here. All right? So that's another cool reason we do this kind of thing.
So we're going to use TextField right here for the body. And again, we can set a max length, but I'm not going to do that for the body. And finally, we need to use the DateTimeField for this. Awesome. models.DateTimeField. So this is going to grab whatever the date and time is that this instance was created when we create a new article. All right, and inside here, I'm going to pass through an argument which is called auto_now_add, and that's going to be equal to True. And what that does is when an article is created, it's going to automatically populate this field with the time now. The user doesn't have to input the field, it's going to automatically add that for us. Okay.
Next Steps: Making Migrations
So this, my friends, is now our model so far for an article. Okay? And so the next thing we need to do is make a migration so that this model is then mapped to a database table. And we're going to do that in the very next tutorial.