- Overview
- Transcript
2.1 Creating Models
In this lesson we will focus on creating Feed
and Article
models to represent news feeds and news articles. Models in the world of Django are classes that describe objects in your application. They also provide the basic functionality for persisting this data within any sort of database that you choose to use in your application.
1.Introduction3 lessons, 14:42
1.1Introduction00:58
1.2Prerequisites04:14
1.3Set Up the Development Environment09:30
2.Foundations6 lessons, 47:44
2.1Creating Models09:20
2.2Migrations and Administration08:04
2.3Create Router URLs10:25
2.4Our First View04:48
2.5Create a Template05:46
2.6Adding Dynamic Data09:21
3.Add Structure6 lessons, 52:54
3.1Incorporate Bootstrap06:10
3.2Add a Feeds List Page11:11
3.3Add a Base Template07:10
3.4Create a Feed Form09:28
3.5Handle POST Data06:07
3.6Adding a Real Feed12:48
4.Clean Up3 lessons, 16:58
4.1Give Models a Meaningful Name04:24
4.2Format the Articles Page08:49
4.3Check for Duplicate Data03:45
5.Conclusion1 lesson, 01:23
5.1Goodbye01:23
2.1 Creating Models
Great. We've now been able to successfully create our project and we've been able to kind of get that first version, or the initial state of our database, out there and see our application in the web browser. And that's all fine and dandy, but the problem now is there's really nothing there, so we need to start giving it Something, something in terms of what is going to be represented in the database and what sort of objects or models that we're gonna start referring to within our application. But, let's take baby steps. So the first thing that we wanna do is we're gonna come back into our command prompt and we're going to quit our web server. So we're going to use control c to quit. And now what I need to do is we've created a project, so we've created our news project but now we need to create an app within that project that's ultimately going to contain a lot of the logic and the models that are going to be represented within our app, ultimately. And so what I wanna do is I need to create this app. So the way that we do that is we say python. We wanna do our manage.py file again. And this time, instead of using start project, we're going to use startapp, and then we're gonna give it a name, in this case, we're just gonna call it news. So that didn't really take very long, it doesn't really seem like it did very much, but if you start to take a look at your directory structure now, we now have a news folder, so I can go into there, and I can take a look, and I have a number of Python files in there, and I also have a migrations directory, which as of right now there's really nothing in as well. So this is all gonna change very momentarily over the next couple of lessons, but this is the starting point of our app. And really, just to reiterate again, an app within the world of Django is nothing more than a logical structure that's going to allow us to create some functionality and represent some data within ultimately are entire web application. So let's go ahead and go back a couple levels just so we're living at the root here and now what I wanna do is I want to go into my text editor and I want to start creating some code. So, what you're going to notice here is I have my news directory and I have my migrations folder, that's empty right now, and I have my other Python files and the one that I'm caring about at the moment is models. .py. In here, this where I'm going to create my models. And what is a model? Within the context of Django, we are talking about a class that's going to represent some real world data within our application that we can refer to. But then also it's going to model what are database structure is going to look like. So it's kind of twofold. So we're going to start to define the models that are going to be necessary for us to create this news aggregator. And we're gonna deal with two. We're gonna start with a feed, which is going to be our parent model that is really going to be Where we're gonna start. We're going to specify where the URL is that's going to define all of the articles and things that are within our feed, and then we're also going to have a sub-model or a subclass. Called article that's contain a series of news articles and things like that that we can then start to display to the end user. So let's begin with the first one, called feed. So we're gonna create a new class. This one is going to be called feed and it's going to be a models.model class, so basically that means that this feed class is going to inherit from model which is going to tell Django that this is a model class that when we start to create some migrations, which we'll talk about in the next lesson, this is what it's going to look at. It's going to find all the classes that are feeds. And then going to work those into the database. So now we can define some of the fields that are going to exist within this model. So the first one we're going to have is going to be a title and we're going to set that equal to to models dot, and this is going to be a character field which basically means this is going to be a string and then we'll specify a max_length here of 200. So I'm hoping that the titles, which should be relatively short when it comes to these Feeds themselves should be relatively short. So I think 200 should be fine for now. And that's gonna have a URL, so we're gonna say models.URLField, so this is gonna be the URL that's going to contain all of the feed information as well as its associated articles that we find out on the Internet. And then we're gonna create an is_active flag. And this is_active flag is going to allow us to determine what is ready to be presented to the end user. So if we first create a feed and we haven't actually went out and grab all of it's information yet. Then we're not gonna present that to the user because there's just nothing there. This is also going to allow us to say if we wanted to remove some publication or we done want to present it anymore but we still want to contain it within our application then we can just set this flag and it will allow us to kind of remove it from the view. So just a little nice piece of functionality. So we're going to set that to models dot. This is going to be bullion field and we're going to set the default equal to false. So when we first create a feed we don't necessarily want to present it because we may not have all the articles for it yet. So we're just going to kind of leave it as inactive. So that's gonna be our first class our feed class. Now our child model or our child class is going to be an article. So a feed can contain many articles and then an article belongs to a single feed. So we're gonna create another class this one's gonna be called article. Once again this is going to be a model class. And then we can define some very similar fields or characteristics about the article. It's also going to have a title, and once again we'll say that this is going to be a character field, or a string, and this is ultimately supposed to, or hopefully going to have, a max length of 200 again. Now, this one can have a couple different properties but we're just gonna keep it fairly simple for this. And you can definitely add to it as much as you want. But it's also going to have a URL. So we're going to have models.URLField like that. Then it's also going to have a description and that's going to be models., we're going to use TextField for this because text fields are typically used for fields within our database, ultimately that there is going to contain larger Text content. And then we're gonna have a publication date which is gonna be models.DateTimeField. Like that. So that's the basic structure of both of these model classes. But now, there's really nothing that's tying these two things together. There's no relationship Between them. And the way that we gonna do that. At least for this particular case, is we're going to create a foreign key relationship between the two. So I'm gonna stay within my article class and I'm going to now specify the fact that this article is going to be related to a specific feat and the way that I do that is I"m going to give it a property or a field called feed and I'm going to set that equal to models dot foreign key and it's going to have a foreign key relationship to our feed class. Like that. So we have each of our classes created, each of our model classes created. So I can go ahead and save that. So that's great. We've created these models, but now what do we do? Well, before we can do anything with them, we have created this concept of a news app represented by this folder here, but just by doing that we haven't actually told our Django project that this thing exists, and this is one of the things that you kind of have to keep in mind when you're first starting. Django development is that a lot of these things are automated and they do create these kind of boiler plate scenarios for you. But not everything is taken care of. So in order to actually register this app within our project. We have to come within our project directory. Go into settings. And I'm gonna come down here a little bit to installed apps. And as you see here, there's a number of installed apps that are available to us within our project by default. But when you first create a project like we did called news I have to manually add this in here. So this is gonna be called "news." So every single time you create an app within a project, you're going to add a new entry into INSTALLED_APPS, delimited by commas, and you can add as many in here or as many apps that you create in your project. You need to register to them within installed apps. So we'll go ahead and save that as well. So now we've modified our settings file to let Django know, or to let our project know that we have a new app, and we've created some models. But what now? So how do we actually see what's going on? So what I'm going to do in the next lesson is I'm going to introduce. The ability to take this models and transform them into something within our database and then be able to view kind of what's going on within this models, within a database from an administrative perspective