What are Django Models?
Alrighty, hosses, welcome back. In this video, everything is going to start to make sense and come together, and hopefully you guys are going to have a lot of "ah, I see what's going on now" those kind of moments. So what we're going to be doing is we're going to be creating our models for our music app.
Now, a model is just a blueprint of how you want to store your data. So what kinds of data are we going to be saving? Well, we're going to be saving albums and songs. So we need to build blueprints, and for example, we're just going to say, okay, the album needs a title and a genre and like an artist and stuff like that.
Now, if you guys are like, alright, you're going to create these blueprints, but are you talking about like for the database or for your actual Python code? Well, this is the cool thing. Whenever you make models in Django, it's the same thing. You don't have to make one for your database and then say, okay, now I have to configure this with my actual code. No, no, no. You just make a class right in here, and automatically what Django is going to do is it's going to get each variable from those class and convert it to a column in your database. So it's actually pretty amazing.
Creating the Album Model
So open up music/models.py, and we're going to create our very first class, which is Class Album. Now every model or blueprint you create, it has to inherit from models.Model. Alright, so again, all you do inside these classes is you're just going to create variables. So what does each album have? Um, let me just go ahead and write artist equals...
And this variable name, again, we're just writing a regular Python class, so again we can just work with it in Python. But later on, whenever we migrate over to the database, this is actually going to be the same name as a column. So we don't have to write it twice. Django takes care of all of that for us. Pretty sweet.
So after this, what we do is we actually set this equal to models and then the one thing that we need to tell Django is what type of data is this variable going to hold? Is it going to hold text, or is it going to hold a float, or is it going to hold an integer, or maybe something weird? Well for this tutorial, I'm just going to keep things as simple as possible. And of course, the artist's name is just a bunch of text, so that would be a CharField.
The one thing that we need to specify whenever we are storing characters or just text is just a max_length. So we'll say max_length equals, I don't know, how long are people's names? 250, I guess. I don't know, they probably aren't even close to that long, but whatever. Alright, so for each album, we need to go ahead and write the artist's name, whether it's a band or, you know, just like an artist like Taylor Swift or whatever.
And what else? Let's just go ahead and write album_title. So what was Taylor Swift's last album? I don't even know what it was. I know Red; Red was one, and then she had another one named Taylor Swift. So, uh, yeah, so that's what, you know, would be an example of this. And just like before, this is also just going to be characters. And I don't know, is the album title the same length? Maybe they're longer, so we'll say that we can have 500 characters for that.
Now another thing that we can do is store the genre, whether it's country, rap, whatever. So models.CharField(max_length=100). I don't think any genres are more than 100 characters.
And I actually thought it would be pretty cool if, whenever we were making our website, if for each album then it had a logo. But I didn't teach you guys how to upload files yet. Eventually, we're going to be doing that. So right now we're just going to have to post a link, and that's how we'll display it. We'll just find some image online and use that link of the image. So I'll just say album_logo = CharField and we'll say max_length equals a thousand since maybe some of these URLs are going to be kind of big.
Alright, so this is what an album is going to be made of. So whenever we're using it in Python, we just use these variables whenever we have to, you know, like set that album title or something. And also later on whenever we look at this in the database, the actual column name is going to be artist. So all that is taken care of behind the scenes. It's going to be cool.
Linking Songs to Albums with Foreign Keys
Now after this, I'm going to go ahead and create one more class and I'm just going to name this Song. So just like before, and since I'm too lazy to type all that, I'm just going to go ahead and paste that.
Now, check this out. So we actually need to associate these in some kind of way. So we can go ahead and create as many albums as we want and they can just be, you know, kind of their own thing. But whenever we create a song, it needs to be associated with an album. In other words, a song needs to be part of an album. So usually we're going to have like 12 or 15 songs per album. So how do we link these together in that kind of way? Well, what we can do is this: if we make a variable called album, we can go ahead and write models and instead of just CharField, we can write ForeignKey.
Now let me go ahead and type this real quick and I'll explain what this means. on_delete=models.CASCADE. Alright.
Primary Keys vs. Foreign Keys
So, and I'm getting alright. So first of all, let's go ahead and talk about primary keys. You know that I said that whenever we create this in our database, we're going to have these four columns right there, but actually behind the scenes Django is going to make another column for us, and this is pretty much a unique ID number. So the very first album we create, it's going to give it the ID of one. When we create another one, it's going to give it the ID of two, so on and so forth. And each additional album that we add, it's going to increment it by one.
So why does it do that? I mean, whenever we just try to pick out an album out of there, we can just say, "Hey, give me that album Red." Well what if not only Taylor Swift has an album called Red, but a couple other bands do as well? Well, then it's going to get confused. Alright, so let's just say, okay, give me the albums by Taylor Swift. Well, Taylor Swift probably has like 12 albums or whatever, so it's just a mess if you have all of these pieces of information and you don't know which one to specify.
So whenever you have a row in a database, it's always good to have a unique ID number. Now since one album can only have its ID number, in other words, two albums can't both have the ID number of five, that's why you call it a primary or unique key. So that's what that is and that's what these albums are going to have behind the scenes. Alright so what is this foreign key? That's kind of weird. Alright, so each song is going to be linked to an album. So we'll say that the album Red has the primary key of one. So basically, whenever we create a song, say Taylor Swift, I don't know, like, "I Hate My Boyfriend" or whatever, we need to link it to an album. So the key, the foreign key for this is just going to be one, and that way we know that this song, whatever song we said, is linked to the album one, which is Red.
So again, a primary key is just needed whenever you have a bunch of elements and they each need a unique ID number. A foreign key is used whenever you have something like a song that's part of something else. So you just give it the ID number of whatever it belongs to. And that's what a foreign key is.
The on_delete=CASCADE Behavior
Now the only other thing you guys may not know is this on_delete=models.CASCADE. What this means is, alright, since you're going to have a bunch of songs and they need to be part of an album, what happens whenever you delete that album? Well, you just can't have a bunch of songs still, because again, that doesn't make sense. You can't have songs that aren't on any album, at least not in this, you know, kind of program.
So we're going to say models.CASCADE on delete, which means whenever we delete the album Red, any songs that were linked to that, then go ahead and delete those as well. And you actually can set it up in some programs where you can say okay, it's actually okay to delete these things and these things might still exist. Um, I don't know, maybe you had like a forum post and you wanted to delete it but all of the answers are really good so you wanted to keep them or something. So that may be an example, but I don't know, I'm getting kind of ahead of myself, anyways.
Finalizing the Song Model
Moving on. Now that you guys had that little primer. Alright. Another thing I want to store in here is a file_type and this can just be like... MP3 or, I don't know what's another one? A WAV file? Whatever weird ones Apple has. And we're just going to store those as... okay my caps lock is on and I'm too lazy to do that anyway.
So a file_type, MP3, WAV... I don't think any file types are longer than 10 characters so we should be safe there. Now the last thing we should probably have is the song_title. That's pretty important, eh? So models.CharField, max... how long is the title? 250. If any title is longer than 250, then whatever.
All right, looking good, mate, looking good. So albums, we have all the information that we want in there. Songs, okay, we have the album that it's linked to, the file type, and the song title. Looks good. So, alright.