How can you set up the database in Django step by step


How Can You Set Up The Database In Django Step By Step
How Can You Set Up The Database In Django Step By Step
How Can You Set Up The Database In Django Step By Step
  • Django offers its own default database of sqllite3. You can use any other databases by installing the databases bindings.
  • And change the default in DATABASES to django.db.backends.postgresql if you are using sql or replace it accordingly.
  • Now before using the database we have to create tables in the database because the different apps in INSTALLED APPS will probably use it.
  • So to make the table we have to run migrate command “python manage.py migrate” it will look in INSTALLED APPS setting and create tables that are necessary according to the settings.py file.
  • So to make the table we have to run migrate command “python manage.py migrate” it will look in INSTALLED APPS setting and create tables that are necessary according to the settings.py file.
  • Now we have to define models means we define the fields and the behaviour of the database. Now we can use the class variables present in models class to define the layout of the database for example: autofield which can be used to define product id and charfield to define product description etc.
  • Now we have to include this in our project by adding a reference to its configuration class in INSTALLED APPS setting.
  • Now we have to tell django that we have made some changes to our models and want to make these changes in database but django doesn’t directly make changes in the database instead it stores these changes as migration and to do this we have run a command “python manage.py makemigrations” but remember this does nothing to the default django database.
  • Now we have to tell django that we have made some changes to our models and want to make these changes in database but django doesn’t directly make changes in the database instead it stores these changes as migration and to do this we have run a command “python manage.py makemigrations” but remember this does nothing to the default django database.
  • Now that we have made models and their migrations in djnago its time to make in changes in database accordingly and to do that we have to run a migrate command but its different from the above, the above was migrations and this one is migrate. Now run the command “python manage.py migrate” this will make changes in your database according to the models that you made. By using migrations you can make changes in database without changing the whole thing.
  • Now that we have setup the tables its time to play with django database APIs, to do this run this command first “python manage.py shell” what this does is it opens the special django shell not the regular python shell. Now import the classes (which is table in the database, and the class variables are the elements of the table in the database). Now create a object of the class and pass the values of the variables and run object_name.save(), now you have a one element in the database and it has a id or a primary key(pk) as 1 and you can that using object_name.objects.get(pk=1).
  • Now make a super using django admin and setup the password and import the classes from the models package and pass that class from “admin.site.register(class_name)” in admin.py file. Go to the admin the site and add data in database directly.

Summary :

In this article we saw How Can You Set Up The Database In Django Step By Step so about this article you have any query then free to ask me

Name of Intern who share this Task : ANAND CHOUDHARY

github account : https://github.com/anandchaudhary854/ 

I am Mr. Sachin pagar the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *