7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.1 Give Models a Meaningful Name

You have probably noticed that it's hard to identify individual feeds and articles in the admin interface, because they all have the same name. This will quickly become a problem if you are trying to do any sort of administrative work on the back end. In this lesson, I will show you a quick fix to get more meaningful names for the objects in the database.

4.1 Give Models a Meaningful Name

So, in the previous lesson we did a very good job of finally starting to download some meaningful data and actual news feed parsing the information out, using feed parser and then presenting it in a very simple way to the end user. And we're gonna kind of clean that up a little bit here in another lesson, but what I'd like to focus on right now is that if I were to come back over to Chrome and take a look at my administration site and go into articles. You're going to see we have a bunch of article objects, which is incredibly ugly and very difficult to maintain and we're going to have the same problem when we talk about the feeds. Now right now because we're only really worried about getting some information up to the end user. This might not be a huge problem, but it's gonna become a problem if the site begins to grow. How are you supposed to know which feed is which? Have you wanna go in and edit something, or you're gonna have to go in and out of all these feeds and articles just to find what you're looking for, and that's a big maintenance nightmare. So what I would like to do is show you where this information comes from and how we can fix it. So if we head back over to our text editor and look at the code that we wrote. Really all we've done here is we've created new instances of some model classes that we defined in a previous lesson. So, let's come over to our models. So, this is where the magic really happens. And this is where we have defined the models within our application that can store information and be represented within the database. But, these are just very vanilla classes, there's nothing special about them. And the problem that we're running into on the administration site, is the fact that when Django tries to give you a string representation of an instance of one of these models, it doesn't have anything to go off of. So the best it can come up with is that this is a feed object, or this is an article object. So at least it knows what the type is of the model, but it can't really give you any additional information. But there's a way around that, and it's actually quite simple. And you're gonna laugh once you see this work. So what we're gonna do is we're gonna come into each one of these classes, and we're going to kinda override or define one of those special functions that is kinda a built in that you have access to once you start creating these classes. And that's going to be the __STR__. And this takes in a self parameter because this is an instance method. And, so what this is going to do is it's going to return a string representation of whatever object or whatever instance of this class. So in this case I'm simply going to return self.title just like that. And then I'm also gonna do the same exact thing for article. And paste, and save that. So just that little tiny change is gonna make a world of difference. Anytime any sort of application, whether it be the administration page, or site on your Django application, or just using these feeds and article models at the command line if you were using the Python interpreter. So simply making that little change, I can head back over to Chrome. And I can come in here and I can go back into my administration site and click feeds, and now all of a sudden I don't have to reload any data. Just by making that little change Django has been able to say now I know how to give a string representation to those objects. I'm now dealing with the My Blog, which is that basic one that I put in by myself. And then I had the Django web log which is what we pulled down from the internet. Same with articles. Now I can come over and I can see all my different titles of those articles. I can select one of them and not only does it make it easier to read there, but now when I can see the associated feed and I can change those back and forth or whatever I want to do. So just by making that little change by adding that implementation for the _ str function I'm now able to see the actual data on the administrative site. And now that we've come this far, we don't really want to stop here. We could just kind of leave it as is but I think we could do a little bit better. By adding a little bit more formatting instead of just presenting this as an unordered list. Maybe starting to treat this at least maybe a little bit like an actual news site. Now remember, I am not a designer but I'm just gonna show you a couple little tips and tricks here to be able to present this maybe in a little bit more user friendly way.

Back to the top