Creating a PostgreSQL Database
Welcome back, aliens. My name is Ivan Verdi, and let's continue this series on Django. Finally, we are reaching that point where we are connecting our application with our database. So basically, we want to fetch the data from the database, so that's what we are doing now. In the last few videos, we have talked about Postgres and ORM, and then we have done the setup with the Postgres as well. So if I try to open my pgAdmin, that's how you can access your Postgres using some IDE or some view.
And you can see we have a server option here. So if I expand server, you can see we have a database. And unfortunately or fortunately, we only have one base which is Postgres. Now let's create one more database here. You can do that by right-click on your database and say create database. And let me just have the name of the database as 'telusko'. Postgres will be the owner of this. And let's click on Save. Nothing to specify much. So you can see we got a database which is telusko. And in this, if I see the schemas, tables, there is no table here. So you can see we've got a database but we don't have any table now.
Configuring Database Settings in Django
So let's create some, and those tables should be coming from the application. It's what we will do now.
So if you want to connect your application with this Postgres DBMS, you need to do some configuration. So let's head back to Visual Studio. And here, we need to do some settings. So let's go back to the Telusko project, this is where you have manage.py, and now let's do setting for database.
If you scroll down somewhere, you will find DATABASES. Right? And by default, it says it will get connected with SQLite3. Hold on, we are using Postgres here. So we don't want to specify SQLite3 here, right? So let's make the change. So here, we'll say backends, it should be Postgresql. Database name is telusko. So we don't need to specify all this stuff. We can simply say telusko. That's my database name. Let me give a comma here.
Now we have to specify something more because if you want to access your Postgres we have to specify the credentials as well: the username and password. So let's do that. So in single quotes, I will say who is the user. So if you remember, we have talked about user as well. And that is your Postgres. This is what we have seen while installing Postgres. You have to also mention the password here. So we say password. In fact, I have specified a password which is 1234. And then we have to also specify on which machine you have your database. It is possible that in your network, you have another machine in which you have your DBMS, or Postgres installed. So you can specify the host. And this is where you can specify the address, I mean the IP address of that machine. Time being, it's the same machine so I will be using localhost. So this is the configuration you have to specify here to connect with the database. So this configuration is done.
Installing the psycopg2 Adapter
Okay, but there is one problem. Postgres being a different software, Django being a different software, how they will connect? So basically, we need a connector in between. And the connector name is—if I go to Google and if I search—so the connector we are going to use is psycopg2.
So if you see, it's basically a database adapter. So psycopg2 is an adapter which connects with Python. So you can see it says it's a connector or the adapter between Postgres and Python. So this is what we wanted. So how do we install this? So we have a simple command which is pip install, and then you can specify the name. So let's head back to our prompt here and we'll say pip install this software name. So we say pip install the name of the package and say enter. And this will get downloaded. It will take some time. And it's done. It was quite fast. So you got the adapter as well now.
Creating a Django Model from a Class
What's the next step? Now basically, if you remember, we have talked about ORM. So Django has this amazing feature of ORM. It was to create the database tables for you. For example, for authentication, if you have your own models, it will create the database there.
So let's try to migrate, but before migration we need one thing, right? We need a model. So the table which you want, which is Destination, will be dependent upon your model. And you will say, "Hey, we have a model file here, right?" So if you see, we have a models.py. This is where you got your model. But no, actually, this Destination is a simple class. You want to convert this into a model. This will not work. So how do we do that? So it's very simple, actually. So if you want to convert a class as a model, if you want to give that more power, we have to specify models.Model. So basically you are inheriting from the models there.
Now, will this work? No, still it will not work. Because, see, when you say you are creating a table, the table will have a type of columns as well, right? So if you see the column name is name, the type would be varchar. That's what we do in SQL, right? Character string, where you mention the size. So here you can't simply say str and int. It does not work in that way. And first of all, you don't even have to mention ID in your model, because in the database it will automatically create a primary key for you, the ID column. So let's remove that.
Defining Model Fields and Types
So I want to change this. Instead of having name, I want to specify the type of the column there. Now, how do we know that? So let's go to Google again and search for Django. So if you search for Django model fields in their official documentation, they will help you with this. You can see on this side, we have Field types. The field types are AutoField, BigAutoField, and then BinaryField. We have CharField, that's for string. We can also have EmailField, we can also have ImageField if you want to upload an image. So you can see we have different types here. And how do we use each one? So if you just scroll down, they will also help you with the format. So, yeah, so you can see we have AutoField. This is how you should be using it. They also have the examples here.
So if you want to use CharField, you have to say CharField and you have to specify the attributes as well, the max_length. Let's use that. So here, the name would be of character type. So we say models.CharField, and in here you have to specify the maximum length. So I would say max_length, and the length would be, let's say 200, or maybe 100 will do. Nobody has a big name more than 100, right?
We'll do something for image later, but as of now, let's stick to description. And in description, you will say again, models.... Okay, but what should we be using for description? Description might be big, so we'll be using TextField here. We don't have to specify the size. For price, it should be int. So I will say models.IntegerField. Do we have IntegerField? Yeah, of course, it should be. So IntegerField. And for boolean, you guessed it right, we should be having BooleanField. That job is done. So simple, right?
But then when you say BooleanField, by default it should be something, right? By default, there should not be any offer on the holy places. So by default, the default value here is, let's say, False. Anything else? We need to specify for image as well, right? We are missing image now. So in the image, you have to upload the image, right? It will not be stored in the database. When you upload an image, it will be stored in a folder and you have to specify the path here. That's where you have to specify the path. The path is important. So what I will do is I will say models.ImageField. So for the image, you have to use ImageField. And in here, you have to specify the location you want to upload to. So for example, if I go back and if I say ImageField, so you can see in the ImageField we have to pass upload_to as the attribute. So I will say upload_to, and let's say the name I will say is pics, a folder named pics. I will upload everything there.
So there is one particular problem which everyone does. In fact, I did that in my first project in Django. And the problem was, normally when you specify the field type, you give a colon and the field type. But here, now, since you are working with models, you have to give equals to (=) because you're assigning the values here. So this is important because if you miss this, it will not create any column in your table if you don't specify =. So that's done. I think it should work now.
Troubleshooting and Running makemigrations
But how do you verify this now? First of all, if you want to create a table, you have to migrate your models to the database. And for that, you have to pass a command. So what is that command?
So let's go back here and say python, of course you have to use manage.py. The command is... the first thing you have to do is you have to make a migration. Because if you see a folder here in your travello app, you have a folder which is migrations and it is empty. Yeah, we have one __init__.py but we don't have the migration file. So let's get a migration file. So we say makemigrations. This is the command you have to pass. Say enter, and it will create a migration for you.
And... okay, so you can see there is an error, and it says the travello model Destination is not available in the installed apps. So if you go back to settings.py, remember we have talked about INSTALLED_APPS, or maybe not. So you have to mention your application name here, whatever you are working with. So let's do that quickly. So the app name is so travello.apps.TravelloConfig. This is what you have to mention here. So your project should know what apps you are working with. See, this is important. So if you don't do that, it will not do the migration. Let's do the migration again.
Let's do makemigrations again. And we got an error again! We are missing a comma. Can you believe that? Let's go back. Okay, let's say CLS and if I say makemigrations... Okay, there's another error now. Now this, this error makes sense. I was expecting this error.
Anyway, you want to work with images in Django, and since you want to upload them, there should be some library to handle that. And that library is Pillow. So you have to install this as well. So let's do that quickly. So we'll say pip install Pillow. I know, there are so many softwares you have to install, but everything will be done only once, right? So let's do that quickly. So it's installing Pillow now, and that should be done fast. Right. Okay, finally we can run makemigrations. I hope this time this will work. Let's enter. And it worked!
Inspecting the Migration File and SQL
Can you see that? We got a migration done. And in your migrations now, so if I expand this time, yeah. So you can see we got one more file which is 0001_initial.py. And if you open that file, this is the migration you are doing. So basically, you are doing a migration which is CreateModel, and these are the fields you have. And you can see ID. Even if you have removed the ID, by default it will give you an ID which is an AutoField and auto-increment field. And you got name, image, description, and offer. This is what we wanted. So everything looks cool here till this point.
But what do you think? Have you got a table? Unfortunately, no. We have just created a migration file. We have not actually migrated something. So if you want to see what the SQL is, you can simply say python manage.py sqlmigrate and you have to mention the package name which is travello, and then you have to mention the migration file name, which is 0001 in this case. So if you say python manage.py sqlmigrate the app name and the migration number, enter. So this is the SQL query it is going to execute. CREATE TABLE travello_destination, the primary key, and then the columns. You don't have to write the SQL query, right? Yay! Okay.
Applying Migrations and Verifying the Table
Now let's do the actual migrations. So I will say python manage.py and simply say migrate. Simple command, migrate. Say enter. And you can see, done. No errors. That's something amazing.
Let's go back to our browser... in fact, pgAdmin. I hope we will be getting tables now. Let's refresh. Or maybe I will simply right-click here and say 'Refresh'. Expand this. Yes! We got tables. So all these tables have been given by Django. We don't even need them for now. We wanted this travello_destination, and you got it. Okay, we got a table. But if I want to show the data here, so if I say 'All Rows'... okay, this is empty because we have not entered data as of now. But you can see that we got a structure now. You got a table from a class. That's the magic of ORM, right?
So this makes sense. You got the setup and everything is working as of now. So we have done the migration. But what's more important is we want to add data, maybe with the help of some admin panel, or maybe with the help of some page. But we will do that later. So that's it from this video. I hope you are enjoying this series. Let me know in the comment section.