Understanding and Creating Models in Django

Search for a command to run...

No comments yet. Be the first to comment.
This is a basic Django tutorial in which we will create an Authentication App, from scratch in Django 2.0 and Learn the Fundamental concepts of Django Framework.
Django Forms In this tutorial, we will learn what are Forms in Django and how to use forms.py to connect with models to directly save the result into the database table. So in our previous section, we have successfully created our User model. Let's s...
You've done it. I've done it. We've all done it.

Your AI Agent Has Amnesia. ByteRover Fixes It — Without Vectors Let's see how to built a customer support agent that remembers everything — using plain markdown files, BM25 search, zero embeddings, a

My GenAI cohort kicked off today, and what a first day! Right away we were reminded that AI isn’t magic — at its core, models like Generative Pretrained Transformers (GPT) are doing one simple thing:

Hello Folks👋, I Recently got Graduated🎓 in 2020, yep in the Pandemic. We all hit very hard by this Corona Pandemic and we all got stuck at home for very long. We all realize the Importance of Socializing, Fitness (Physically as well Mentally), Diet...

So after learning the basic commands of git in our previous section, let's see Git in action. If you already have a project to track your files then you can skip this section and if not then continue. Initializing a Repository from Non-existing Dire...

In this section, 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 section, we have successfully configured our account app. Let's see what are Models in Django.
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)
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.