Debugging: Part 2
In the previous lesson, you saw that
FilePathField() might not be the best choice for your model, so you’re going to change it:
image = models.CharField(max_length=100)
When you check your page again, you’ll see that nothing changed. This is because you just made some changes to
models.py but didn’t modify the database. In order for the changes to be made in the database, you need to
So we want to go ahead and change this. However, there’s a couple of things that we have to consider when doing this, so let’s go over it step by step. In our case, we’re really just saving a couple of characters:
/projects/img and then the filename. So I will change this to be a
CharField (character field), instead.
We can get rid of all of this and just give it a
max_length argument. Our path shouldn’t be—they are always going to be
projects/img/ and then the name of your project, so they should never be longer than 100 characters. Okay.
00:57 And you might wonder: why is that the case? I mean, in this case, we actually didn’t change anything, we just changed the data type, right? But also, we did not do any changes to the database at all.
We just did some changes to the
models file, but that doesn’t mean that the changes are propagated to the database. So, in order to make these changes to the database, remember, we have to do two things. First, we have to
makemigrations to create the migrations file.
And let’s take a look whether that changed something here. Here it is! Now we’re displaying the right path:
projects/img/testproject.png. Django can find the static file and finally can also display it.
Our second one still does not show up, and the reason is because we haven’t changed the database entry yet. I’m going to go ahead and do that for the number
2, also. Let’s choose a different one—maybe that
Make sure that you save it. If I do not save it and go back over here, we did not yet apply the change to the database, so nothing changes here. However, once I go and say
.save(), my changes that I did here in the console are written back to the database and now we have a different path in here and finally, we’re able to display that image. Great!
05:00 Now it starts to look a little bit like our final version. We went ahead and did an intervention inside of our models. Like, we went back into the models and changed something, and we went to directly interact with our database using the Django shell and changed entries in there.
We simply went and changed this
FilePathField to a
CharField, made the migrations, and applied the migrations to the database and everything was fine. However, in some cases, this is not going to work as easily, and that’s when you’re actually changing the data type.
FilePathField is also just a character field, so there were no big changes that we had to do, but if you already have something sitting inside of your database and you apply some changes like this, you might run into some errors from Django. What you can always do—especially when you’re working on a small project and if you’re still in development—you can always go ahead and simply delete your database.
That’s often the easiest way: just right-click, delete it, get rid of it, and then make your migrations again. Again,
makemigrations and run them, and Django is simply going to create a new database file for you.
06:24 Of course, it means that you’re going to lose what’s written in there so far, but that’s why it’s important to test often and test early to make sure that the stuff works in a way that it does, and so that you don’t yet have your meticulously-crafted projects in there.
06:38 But we’re currently just working with some test data, and then it’s no problem to start over again. There’s other ways of solving a problem like that but I’m just giving you this tip that often, especially for simple projects in development, it’s easiest to just delete and start over. Okay.
06:55 That’s it for our debugging excursion. We did some cool changes. We made it work. We can finally see our images and we’re ready to move on to work a little bit more on our templates. See you in the next video.
Become a Member to join the conversation.