Django Tutorial
What is Django?
How Django Works?
Django - Creating a Project
Django- Creating an App
Django- Models
Django- Forms
Django – Views
Django- Templates
Django- URLs
Django- Admin
Complete Authentication System
Setting Up Django Environment Setup
Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Django- Models

February 13th 2020

What are Django Models?
What are Django Models?

Django Models

In this tutorial, we will learn what are Models in Django and how to use models.py to create a database table in Django.

So in our previous tutorial, we have successfully configured our account app. Let's see what are Models in Django.

Models

A Model is a class that represents table or collection in our DB, and where every attribute of the class is a field of the table or collection. Models are defined in the app/models.py (in our example: social/models.py)

Creating a Model

Let’s write the first model. Open the file account/models.py and put the following Python code in it:

from django.contrib import auth
from django.db import models


class User(auth.models.User, auth.models.PermissionsMixin):

    def __str__(self):

        return "@{}".format(self.username)

So in our case, we will have a User table inside the account/models.py file.

Every model inherits from django.db.models.Model. That small bit of model (User in our case) code gives Django a lot of information. With it, Django is able to:

  • Create a database schema (CREATE TABLE statements) for this app.

  • Create a Python database-access API for accessing User objects.

Now, run the following command:

python manage.py makemigrations account
You should see something similar to the following:
altaf@kali:~/Desktop/Tutorial/social$ python manage.py makemigrations account
Migrations for 'account':
  account/migrations/0001_initial.py
    - Create model User

By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.

Migrations are how Django stores changes to your models (and thus your database schema) - they’re files on disk. You can read the migration for your new model if you like; it’s the file account /migrations/0001_initial.py

and then run:
python manage.py migrate
altaf@kali:~/Desktop/Tutorial/social$ python manage.py migrate
Operations to perform:
  Apply all migrations: account, admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying account.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying sessions.0001_initial... OK

The migrate command takes all the migrations that haven’t been applied (Django tracks which ones are applied using a special table in your database called django_migrations) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.

Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones - it specializes in upgrading your database live, without losing data. To make changes in the model remember the three-step guide :

  • Change your models (in models.py).

  • Run python manage.py makemigrations to create migrations for those changes

  • Run python manage.py migrate to apply those changes to the database.

Hurray! We have successfully created our first model in Django. Now, Next we will move on to the forms (forms.py) in Django.