Subtitles section Play video Print subtitles what's going on? Everybody welcome to part two of the Web development with Django and Python Knowledge Transference. Okay, in this video we're gonna be talking about is models. We're just gonna start getting into models and how we can interact with them. But pretty much models with Django is where you're getting almost all of the value from the jangle Web framework is coming from models and all basically the abstraction from those models. So, um, this is another one where I think you you would You might want to spend a little more time making sure you understand it really well before you move on to the next topic. So with that, let's get into it. So again, models air just the way that your website or your AP gets to interact with the information stored within your database. So, for example, on our website, we might want a store like our Well, for sure, you're gonna start users to your database you're going to store. Um, so, like, for a user, you might you might score like a user name thing, actual person's name and email password, all that kind of stuff, and then, in our case, too. We would like to likely store our actual tutorials to the database. We will get to users, I promise. But that's a that's a more complex issue because you got to do the registration log in and sessions and hashing passwords, all kinds of stuff and forms and stuff like that. So for now, we're gonna try to keep it more simple. We're gonna focus first on just a simpler tutorial model. So a tutorial model? What kind of aspects are attributes with a tutorial? Have we've got, like, the editorial content, So this is just, you know, the actual body of the tutorial. But then you've got, like, maybe a tutorial title and then the published dates, maybe a playlist like Kara Siri's that it's a part of and then maybe like a category or or something like that. We were probably want to all those yet, but eventually that might be what you would use now in terms of other. Like if you were just trying to come up with some way to store these things to your database, you would really want to spend a lot of time before you build your actual database out. You'd want to spend a lot of time thinking ahead. Like, what might I need in this database? Because you're not gonna want to migrate a database. But but with Django really is so simple that you don't need to think about everything in advanced. Uh, it's not gonna cost you anything, really Toe later. Decide. I want to have this column. I wanna I wanna have this other attributes. I don't really need this one anymore, so I'm gonna get rid of it, That kind of stuff. It just it's not a big deal with Django. So anyways, let's get into it. So with models, we're just going to navigate into the app that we're curious about. So in our case, that's main. We've been working there. I'm gonna close these just to avoid clutter, and we're gonna open up models, not pie. So in here, you've really only got this one import, and that's just import models. And all this is for is every model is going to inherit from Jang Go's your base model. So, uh, what we're gonna do is we're just gonna define a new class, and we're gonna call this class tutorial, and like I said it in inherits from models dot model and all that is el. That means inheritance. If you want, you can go to Python Parliament that go to the intermediate tutorials. You can learn more about inheritance, but basically we are extending this model. So models, that model already has a bunch of different attributes and things associated with it. And then we can just say the little things that we want to change. So, like, what? Columns and stuff like that. Um, but honestly, if you don't know what this means that you don't care to know it's not that big of a deal. You should just know that you have to You have to pass that. But if you want to learn more, it's just inheritance, and you can look more into it. So anyways, uh, zoom in a little bit here. Okay, So what we're gonna do now is just basically specify the columns that we want in our tutorial. So, you know, maybe you're gonna have a tutorial title, okay? And that tutorial title would be models dot car field and we'll give this a max length of 200. Ah, What? I want to look crab for you guys is, uh, like to show you the documentation. So me think you're Django Model Fields? Hopefully that will take us where we want to be. Let me just do a quick search for car field. Looks like it's a pride on this page. Yeah. Okay, so I will put a link to the text based version of the Jewel, for sure in the description. Otherwise, you could just do the same Google search I just did and find yourself here. But obviously, this is quite a large page. There are many different fields that you can have. I'll explain the ones I'm going to use, but just know that many, many more exist, and you should just come here toe, learn about at least all of your options, but the ones were like most people are gonna use, like our field a text employed or something. Text field. And then there's maybe, like a date input type, and then maybe like, numbers. Only stuff like that, pretty much people are gonna use all the same keys, and your pride won't need 90% of the available keys. So, uh, anyways, back to where we were so This is a car field. Max length is 200. Then we're gonna add tutorial contents and that will be models, models dont's text field and then no perimeter. So then immediately the astute among us would be like, Well, Bo, what's the difference between a car field in a text field? So a car, field or care field or whatever you wanna call it Char field. I I have heard people call that a tri field. Anyways, whatever you wanna call it, the car field is basically if if you want to set a maximum, and generally car feels gonna be a shorter thing, whereas a text field is like a blob. Although I bet it would be surprised. Let's see if we search Blob. Oh, really? Text block. Okay. Anyway, I thought they would probably have a unique one for blob, But maybe that's what it maps to an SQL or something. That's just like a text blob or something. Anyway, Text field is like what you would use for a tutorial or an article or something off. Uh, not finite length. So anyways, uh, next thing we're gonna do tutorial content, I'll Let's do tutorial published. Let's be models don't date Time field. And then we're gonna have this called date published published. Okay, so that's enough information. Also, we can we can do various overrides like I was talking about so we can override the string methods so we could return, um, tutorial title. And in fact, first of all, this would need to be self self, My eyes super itchy self. So all we're doing this for is because generally, if we display this object in, like, print out the object, let's say it's going to look kind of ugly. It's gonna be like something tutorial object. And instead, what we should say is, as we generate over it, maybe we want to know the title or something like that, like so it just It just makes it a little better to read. So Okay, so we've got our model now. Any time you create a model, basically, a model is going to map to a database table. So this is our we've got her database already. Basically, you already should have a database. I'm trying to decide where it would be pride back here. Yeah, Here's your database. Traitor. DBS Key light three. The default is sq light later. You can change that if you go toe my sight. Ah, settings dot pie. Uh, and then somewhere in here, it'll be somewhere in here Anyways, over here, probably. Yeah. So this will be your database engine. You could change. It s key. Light is great for development and tutorials like we're doing right now. You can scale and sq light database actually quite well, So I wouldn't you know, the conventional Internet wisdom will tell you I never use sq lighting production. Maybe escalates, actually pretty darn powerful. It's just that my SQL or something like that, or even some of these other databases is probably a better choice in your deployment. But, uh, well, we can worry, even talk about that way later. Down the road, escalate three is nice because it comes with Python. Nobody has to do installation, and everybody can follow along no matter of the operating system, so we'll keep escalate three. And anyways, you already have your table. So what this is gonna do is I'm sorry already. Ever database, What this is gonna do is create a new table tutorial. And then all of these things become your like your columns in that table, and then it automatically just by default gives you a primary key. So anybody who's familiar with making databases and stuff is like, Wait, um, you will already have. Ah, primary. He is just default done for you. But you could also have you wanted set one of these to be your primary key if you so chose. But we're not gonna focus on that for now. So every time you make a new model, that's a new table. Therefore, you have to do two things with Django. One you have to do a thing called make migrations. And all that does is kind of prepare your migrations. Then you have to actually migrate. So it's two steps. So as you've seen already, when we run our server, I'm assuming one is still running. Nope. I guess, uh, I'm not running a server anymore. Uh, S o python managed up. I run server. Uh, it was giving us a message before. Let's see what it does this time. Okay, so it says, you know, you we need to probably run a migrate. Um, this is really only like because we just made this website that we want to run this. So what I'm gonna do, I guess. CD, What do we call this? My sight. Right Eye sight? Yes. So I think it's in here. So what we can do is I just I hate that. I'm just gonna do it in order that you're supposed to do it. We'll do the other migrations at the same time because there was only one time you'll run that migrate first. Uh, So what we're gonna say here is basically every time you add or change, So if you add another model where you want to change a pre existing model, you have to make migrations than my great, and that's basically it, unless you happen to hit an error. So we've made this new model, so the first thing I want us to do is make migrations. So we're just gonna say python managed up high make migrations and it says no changes detected. So what happened here? The reason why I really did do this on purpose. You're gonna hit this. And if I did, it didn't hit it for you. You would be stumped. Or at least if you're like me and you're you're slow. You would be stunned for a few minutes. So what happens is any time you start a new app, we were able to point to that new app. But we haven't, like installed that new app. So first we have to install that new app in order for it to add tables to our database.