A Django model is a table in your database.
To create a model, navigate to the models.py file in the /members/ folder
myproject/tracking_app/members/models.py
from django.db import models
class Member(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
Migrate
Navigate to /tracking_app/ folder and run the command
python manage.py makemigrations members
the output will be:
Migrations for 'members':
members\migrations\0001_initial.py
- Create model Member
(myworld) C:\Users\Your Name\myproject\tracking_app>
Django creates a file describing the changes and stores the file in the /migrations/ folder
myproject/tracking_app/members/migrations/0001_initial.py
# Generated by Django 5.1.7 on 2025-03-20 11:39
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Member',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('firstname', models.CharField(max_length=255)),
('lastname', models.CharField(max_length=255)),
],
),
]
run the migrate command:
python manage.py migrate
the output will be
Operations to perform:
Apply all migrations: admin, auth, contenttypes, members, sessions
Running migrations:
Applying members.0001_initial... OK
(myworld) C:\Users\Your Name\myworld\my_tennis_club>
Now the members table is created in the database
to check the sql statement from the migration run the command
python manage.py sqlmigrate members 0001
Which will result in this output:
BEGIN;
--
-- Create model Member
--
CREATE TABLE "members_member" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "firstname" varchar(255) NOT NULL, "lastname" varchar(255) NOT NULL);
COMMIT;
using python interpreter to add records to the members table
open command prompt
python manage.py shell
You should see something like this
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
At the bottom, after the three >>> write the following:
>>> from members.models import Member
show the content of the table
>>> Member.objects.all()
output:
<QuerySet []>
Add a record to the table
>>> member = Member(firstname='Emil', lastname='Refsnes')
>>> member.save()
show the content of the table
>>> Member.objects.all().values()
output:
<QuerySet [{'id': 1, 'firstname': 'Emil', 'lastname': 'Refsnes'}]>
get the record that need to be updated
>>> from members.models import Member
set the index of the record to retrieve
>>> x = Member.objects.all()[4]
check to see if record is available
>>> x.firstname
Output:
'Stale'
change the value of the record
>>> x.firstname = "Stalikken"
>>> x.save()
show the content of the table
>>> Member.objects.all().values()
Output:
<QuerySet [{'id': 1, 'firstname': 'Emil', 'lastname': 'Refsnes'},
{'id': 2, 'firstname': 'Tobias', 'lastname': 'Refsnes'},
{'id': 3, 'firstname': 'Linus', 'lastname': 'Refsnes'},
{'id': 4, 'firstname': 'Lene', 'lastname': 'Refsnes'},
{'id': 5, 'firstname': 'Stalikken', 'lastname': 'Refsnes'},
{'id': 6, 'firstname': 'Jane', 'lastname': 'Doe'}]>
get the record that need to be updated
>>> from members.models import Member
set the index of the record to retrieve
>>> x = Member.objects.all()[5]
check to see if record is available
>>> x.firstname
Output:
'Jane'
delete the record
>>> x.delete()
output: This show that how many items deleted
(1, {'members.Member': 1})
Update existing model open models.py make changes
tracking_app/members/models.py
from django.db import models
# Create your models here.
class Member(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField()
joined_date = models.DateField()
This will update the table structure of the Member Model.
$ python manage.py makemigrations members
the output
python manage.py makemigrations members
You are trying to add a non-nullable field 'joined_date' to members without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
Select 2 and open models.py
from django.db import models
# Create your models here.
class Member(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField(null=True)
joined_date = models.DateField(null=True)
run makemigrations again and should have this output
Migrations for 'members':
members\migrations\0002_member_joined_date_member_phone.py
- Add field joined_date to member
- Add field phone to member
Run the the migrate command
python manage.py migrate
the output should be
Operations to perform:
Apply all migrations: admin, auth, contenttypes, members, sessions
Running migrations:
Applying members.0002_member_joined_date_member_phone... OK
(myworld) C:\Users\Your Name\myproject\my_tracking>