From the course: Building a Personal Portfolio with Django

Creating the models in Django

From the course: Building a Personal Portfolio with Django

Start my 1-month free trial

Creating the models in Django

- [Instructor] We're now moving to the point in our project where we're going to be working with the database, and the database is where we save information about different things on our website that we want to have persist over time, and one of these is going to be the jobs on our site right? We're trying to display all the different jobs that we've worked before and the database is gonna to be a great place to save this information. So, let's go ahead and move over to Atom where we're gonna be talking about creating a model that will be saved into the database. Now what is a model? A model is essentially just a Python class that can be saved into a database. Django does a great job of transforming our class into the database and back all seamlessly for us. So inside of our jobs folder, which is the jobs app, we're gonna move into the models.py. Now inside of here, like I said, we're just creating a Python class that is gonna be able to be saved into the database. So we're gonna type out here class Job, that's the name of our model, it's not like it has to be the same name as your app, it's just the appropriate name here, and you can have more than one model if you'd like inside of your models.py, but we're just creating our job model here, and inside of the parentheses this is something particular you're gonna have to add as we're taking this models that was imported and we're gonna say models.Model, make sure you get the capital on that Model, and that's gonna be the starting point for this special Python class that can be saved into the database. Now for a job there's really only two properties that exists for it, and I'm just gonna write here in a comment that it is going to have an image kind of showing what it looks like, what it entails, and we're also gonna have a summary so that there's a little text explanation here. So, for the image really all we have to do here is just like a regular Python class, we're going to give a name here like image, and we're gonna say that this is equal to, and we're gonna use that lowercase models up there, and we're gonna say models., and then now say capital ImageField and then in parentheses we have to say where it is that we wanna save images. So but before we say where we're gonna save the image I want to take a step back and say, What have we created here? Well, we've created a new property image that is going to be of type ImageField, and ImageField essentially is some data that's gonna be saved as an image that accepts PNGs and JPGs, all that good stuff, but we do have to specify in here where we want this to be saved, and it's in particular looking for a folder name. So we're gonna say, all lowercase, upload_to=, and then inside of a string here we're just gonna say images/, and this is essentially us just saying, hey put this in a folder called images, and that's all it takes to create a property in order to save an image. Now, next for the summary just like above, we want to say summary=, and the summary is going to be text, so rather than an ImageField in this case, we're going to be using something called a CharField. So we're gonna say lowercase models.CharField, and inside of this parentheses it's looking to say how big do you want this CharField to be. You can't just have a regular old CharField, you gotta say how much space do you want this to take up because this is going to affect how things are saved in the database. So in this case we're gonna say all lowercase max_length and set this equal to some number, now this is really arbitrary, it's up to you. In our case I don't think we should have a summary more than 200 letters because that is just gonna start dragging on and on, and it's going to make the website kind of cluttered. So, we're gonna keep what we have here. I'm gonna go back and delete these little comments that we have because just the name of our properties I think describes what it is that we're trying to do here, that was just a little placeholder for us. We're going to save this file, and if we go ahead and move back to our Terminal here, you can see just by not even running anything, just because the server was running, we get this little error here that is saying hey you need to have pillow if you know doing this, and it looks really complicated. All it's trying to say is, if we move back to Atom here, any time you work with an ImageField you have to have something from pip called pillow installed in order to move forward. It just allows Django to work with images much simpler. So it's going to be a really simple command to fix this. First we're gonna Control+C to get out of the server, and then we're just gonna type pip3 install, and then all lowercase pillow. K, go ahead and hit enter. It's gonna go do it's thing. It's all installed now. So then we just have to run the server again, and look that error disappears, and we can always go back to our browser, we won't notice anything different, but we can see the homepage here. This is a great point for us.

Contents