- Overview
- Transcript
2.4 Creating Models
The foundation of most applications is the model for its data. In this lesson you will learn how to create a special Django class known as a "model". You will also learn how to apply properties to this model that will ultimately apply to tables and columns in a database.
1.Introduction2 lessons, 06:19
1.1Introduction00:53
1.2What You Need05:26
2.Django App Basics4 lessons, 24:54
2.1Install Django and Create a Project05:49
2.2Create an App and Run the Web Server05:15
2.3Responding to Requests06:29
2.4Creating Models07:21
3.Using a Database With Django3 lessons, 16:56
3.1Adding Models to the Database05:28
3.2Working With the Database06:29
3.3The Admin Portal04:59
4.Building Out the Lawn Care App9 lessons, 59:19
4.1Defining the App Routes07:08
4.2Rendering HTML07:27
4.3Rendering Data in an HTML Template05:08
4.4Displaying Available Fertilizers06:15
4.5Saving Time With the Render Function02:25
4.6Handling “Not Found” Exceptions06:26
4.7Using Forms and Saving to the Database09:05
4.8Adding Some Business Logic08:54
4.9Adding Some Style06:31
5.Conclusion1 lesson, 01:06
5.1Conclusion01:06
2.4 Creating Models
Now that we have the basic structure of being able to handle requests into our application, it's time for us to start talking about the actual data that we're gonna be dealing with within our application when it comes to fertilizer and lawn care. And the way we're gonna do that is we're gonna start by defining what are known as models. Now, ultimately, these models are gonna map to what is stored in the database, but we're gonna talk about the database part in just a few moments. But for now, what I wanna do is I want to show you how to define models within your application. And it's actually quite simple. So if you go into your app, so in this case fertilizer, and we come into models you're gonna see this is where you want to create your models. So we can go ahead and get rid of that. So what we wanna do now is I want to show you how we're going to create two classes. And these classes are going to model two things. We want to model the different types of fertilizer that we can apply to our lawn. And we can maybe add those into our application as we go later on so that you know we can continue to use them, maybe other people can continue to use them. And we're going to add some information about them. And then we want to be able to track the applications of those fertilizers to our lawn. So let's create these now. So I wanna create a new class. And I want this class to be fertilizer. And we're going to say that this is going to be models.Model. And this is simply saying that this is going to be a model within our Django application. So we can ultimately map this class into a table in our database. But like I said, we'll get to that point. So now what are the different properties of this fertilizer class that we want to be able to define, that we can use throughout our application? Well, there's a couple things that we're gonna wanna know. Let's say we're gonna wanna know the fertilizer name. And we're gonna set that name equal to models dot and we can specify the different types that these are going to be. So in this case, this is going to be a character field or basically a string. And then within here we can define a couple of things. And then sometimes some of these different types of fields have required parameters. In this case we have a max length is required in this case I think 200 is gonna be enough space to hold the name. Then we want to take care of the different ways that we're going to get this fertilizer, and really, when it comes to fertilizer, you're basically buying it by the bag. So we'll say bag_weight, and this is going to be a models., in this case, we're going to say that this is gonna be an integer field, and in this case, we're gonna define a default here to say the default is gonna be equal to 0. And let's go ahead, close this, so we have a little bit more room. And then the bags typically have a coverage amount. So you can say that a bag of 36 pounds can cover 2,500 square feet. So we'll say bag_coverage is gonna be equal to models and once again, we're going to make this an integer field. And we'll set the default again to zero. So now we have the weight and the coverage which is gonna be important for some of the calculations that we'll do later. There's also a couple of other properties that are important when it comes to looking at fertilizer. And that is going to be the different amounts or percentages of a few nutrients that are within it namely, nitrogen, phosphorus and potassium or NP and K. And those are typically measured in percentages. So in this case will say percent of n is going to be models., in this case, we're going to use a decimal field. And decimal fields you can define other characteristics about them. And you can see there's one of them in here, max digits. So we're gonna say max_digits. Max digits is going to be the number of whole numbers and decimal numbers in there. So if I have a number like 99.99 that's four max digits. And then I can also define another one called decimal_places, and I'm gonna say that's gonna be 2. So in this case, we have a total of 4 digits, which means 2 digits to the left of the decimal point. And decimal_places of 2 is gonna be 2 to the right. And I'm going to duplicate this a couple of times, since we have %n, and we're gonna copy that. And then we'll do two more of these. So we have % N, % P, and % K for phosphorus, and potassium. So these are the important things that we're gonna need to track, when it comes to our fertilizer. So I think that should be enough to get us started. You can obviously change these things, and add as much as you want or use different models or whatever you want. This is going to be the kind of basis of our application or our models. So that's the first one. But now I wanna be able to track the different applications of these fertilizers within our app. So I'm gonna create another class. And this is going to be an application. So an application of that fertilizer. So I'll say models.Model again. And then I want to store a couple of things here, but one of the important things is I wanna track these applications, kind of in relation to the fertilizers. So I can create a foreign key relationship, or I can create a link between these two tables by setting this equal to models.foreign key. So I can create a foreign key relationship to my fertilizer class. And then I can set a bunch of other characteristics but one of the other common ones is to say on delete, and I'm going to set this to models.Cascade. So, now what this is going to do is, it's going to create a linkage between my application model, or table and my fertilizer model or table, and it's going to say on delete cascade. So that means because these applications are linked to this fertilizer there has to be a fertilizer to link this application to. And if for some reason I delete this fertilizer, it's gonna delete all the corresponding applications, which is going to be important as well. Now there's a couple of things I wanna be able to track here, I wanna say bags applied, so that I can apply a certain number of bags of a particular fertilizer to my lawn. And I'm gonna say models dot and we'll go ahead and make this a decimal field as well. And in here we're going to once again define the max digits. And we're gonna say that that's gonna be four and the decimal places is going to be equal to just like we've done before. So I can apply two and a half bags or whatever, however many is gonna be appropriate for my lawn. And I can also say date applied because I wanna know when I did this, so I'll say models dot. Now there's a couple different options I have here I can make a date field which is gonna be only the date portion. Or I can define a date time field which is gonna be the date and the time portion. But in this case, I feel like the date will be enough. I don't need to have anything else. And I could put some other options in there, but I don't really need anything additional for that. So now I have our two models, I have fertilizer and I have application. And this is gonna be important because now we can use these classes. And instances of these classes throughout our application to be able to do some sort of processing. But now in the next lesson, I'm going to show you how to translate these models into tables and things that you could access within the database.







