How to Use the Related Name Attribute in Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in today's video I'm going to show you how to use the related name attribute when you're working with Django models so to do this I'm going to create two pretty simple models one is going to be for students another is going to be for subjects and then the student model will have both a major and a minor on it and they both will use the subjects and you can see how a related name comes into play with all of this so the first thing I'll do is explain what related name is for so when you're using related name this means you're working with a relationship and basically you have a relationship to find on one model but you can also use that same relationship on the other model that it's referring to so if you have a subject for example this is going to be the model that has the related name on it whereas the student actually defines the relationship between the student which will have a major in the subject and on a student I define the related name or as on the subject I will use the related name so this will of course make sense once I create an example so the class is going to be for a student I want this to be models top model and I'm going to give the students a name so named models chartfield say a max length of 20 and I'll create a spare function or a stir method stir and return self name so this is actually going to be the same thing for the subject except it's just going to be called subject instead of student and now I want you to find that relationship so what I'll do is I will say the student has a major and this will be a foreign key field and this is going to point towards the subject table and on delete is going to be models dot cascade so that should be everything I need for this so now let me go ahead and make the migrations for this so everything is saved right yeah so make migrations and I'll go ahead and open up the shell so we can add some data and take a look at what's going on so from example dot muddles I want to import both subject and student and what I want to do is I want to create a few subjects first so we'll start with math subject name equals math we'll have engineering name equals engineering and we'll have one more let's say science subject name equals science okay and I can save all of those whoops save and I forgot to actually migrate so I made the migrations I need some my grade okay so I'll import it from the shell again so from example bottles I want to import subject and student and I'll just create those examples again so subject name equals math engineering and then the final one is science okay so I can save these and science safe okay so I have the three subjects I want so now let me create some students so the first student will be Anthony and name is Anthony and for major I want to put let's say math that was actually my major and then I'll create a second student Christina student and her major will also be math now create a third student Billy whose major is going to be science okay I can save all of these and then I can take a look at the related name that's generated automatically so if I take a student so or excuse me if I take a subject math and I say student underscore set that all this will return all the suits is that have math as a major and you see that even though there is no relationship defined on the subject model I still can call a sub or student said and the reason why I can call student said is because of the related name so the way related name works is like this student sets and this is used this way so math dot student set dot also this student said is like a pseudo column on the subject and then by calling it I can get a list of all of the students that have that relationship so this is all fine and I'll show you how this works for science there's only one student there and then for engineering student sets there's nothing so now let me go ahead and remove this temporarily and if I want to create another one so minor equals models dot foreign key it's going to point to the same subject table it's going to have the same on delete so let me go ahead and try to make a migration for this so make migrations and let's see what happens okay so I have some issues and it is saying that it is clashing with the reverse accessor for minor and major and then let's see if I can scroll back a little bit yes so those are all the errors so it says the hint here is to add our change related name arguments the definition of student dot minor or student dot major and the reason why I need to do this is because I have two foreign keys to the same table and by default the related name is just the name of the model with an underscore set but because I have to I can't use the same name so I need to add a related name only one of them so I'll add it here related name equals and we'll call this minor students so now I should be able to make the migrations there might be one other air and we see the defaults and what I'll do is I'll just set this to null so no equals true and now let me try that again okay so I just made the migrations so now I can migrate and let me open up the shell again so from or python managed by shell from example dot models i want to import both student and subject again and what I'll do is I will add a minor to a student so Anthony is going to be the first one in the database so this is gonna be student objects gets PK equals one so that should be Anthony yeah and I'll also get another subject so I'll get the second subject in the database so first Anthony dots major is math and Anthony minor is blank so subject objects let's just get peak a equals two I think that's science so now I can add science Anthony so Anthony got minor equals science and I can do Anthony dot save and now when I look at the science object I can look at student set again so student underscore sets dot all and I get nothing so that means no students have the major for science but then if I look at this the minor students I just say minor students so this is the related name that I created not all I see Anthony and if I were to add more students to this major then the query set will be longer so if I added Christina for example as science minor then you would see her name in addition to mine so just keep in mind that each time you're using relationships if you have more than one relationship to the same table or you have a need for a custom related name for whatever reason like you just don't like the name of the model underscore set you want to do something else then you need to use the related name attribute so I think it's pretty easy to understand in Django if you have any questions about this you can always leave a comment down below if you're interested in learning more about things that have to do with models in the database in Django for me you can check out my free course called Django database essentials and it has quite a few videos on how to do various things in Django when it comes to working with databases and to join the course you just have to go to pretty brunette comm slash Django data or you can just click the link in the description below I'll add it there as well so that's it for this video if you like this video please give me a thumbs up and like I said if you have any questions about this feel free to leave a comment down below and if you haven't subscribed to my channel already please subscribe so thank you for watching and I will talk to you next time
Info
Channel: Pretty Printed
Views: 11,843
Rating: undefined out of 5
Keywords:
Id: YIJI5U0BWr0
Channel Id: undefined
Length: 10min 55sec (655 seconds)
Published: Tue Apr 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.