Django 3 : Creating our first model

Databases and Django

Django can interface with many databases. However, during the development of our application, we use SQLite libraries that are included in Django.

Step 1 : We will modify settings.py to set our connection to the database:

Creating simple models

Step 2 : First, you need to define a UserProfile model. Add the following lines to the models.py in the myproject folder:

Activating the application

Step 3 : In order for Django to keep track of your application and be able to create database tables for its models, you have to activate it. To do this, edit the settings.py file and add blog.apps.BlogConfig to the INSTALLED_APPS setting. It should look like this:

Creating and applying migrations

Step 4 : First, you will need to create an initial migration for your UserProfile model. In the root directory of your project, run the following command:

Step 5 : Let's take a look at the SQL code that Django will execute in the database to create the table for your model. The sqlmigrate command takes the migration names and returns their SQL without executing it. Run the following command to inspect the SQL output of your first migration:

Step 6 : Let's sync your database with the new model. Run the following command to apply existing migrations: