bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Django/Django Tutorial
Django•Django Tutorial

Django Update Model

Add Fields in the Model

To add a field to a table after it is created, open the models.py file, and make your changes:

my_tennis_club/members/models.py :

from django.db import models
class Member(models.Model): firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField()
joined_date = models.DateField()

As you can see, we want to add phone and joined_date to our Member Model.

This is a change in the Model's structure, and therefore we have to make a migration to tell Django that it has to update the database:

python manage.py makemigrations members

Note

Make sure you are back in the virtual environment before running the command.

The command above will result in a prompt, because we try to add fields that are not allowed to be null, to a table that already contains records.

As you can see, Django asks if we want to provide the fields with a specific value, or if we want to stop the migration and fix it in the model:

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:

I will select option 2, and open the models.py file again and allow NULL values for the two new fields:

my_tennis_club/members/models.py :

from django.db import models
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)

And make the migration once again

python manage.py makemigrations members

Which will result in this

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 migrate command

python manage.py migrate

Which will result in this output

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
\myworld\my_tennis_club>

Insert Data

We can insert data to the two new fields with the same approach as we did in the Update Data chapter :

First we enter the Python Shell

python manage.py shell

Now we are in the shell, the result should be 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 (and hit [enter] for each line):

>>> from members.models import Member
>>> x = Member.objects.all()[0]
>>> x.phone = 5551234
>>> x.joined_date = '2022-01-05'
>>> x.save()

This will insert a phone number and a date in the Member Model, at least for the first record, the four remaining records will for now be left empty. We will deal with them later in the tutorial.

Execute this command to see if the Member table got updated:

>>> Member.objects.all().values()

The result should look like this

<QuerySet [
{'id': 1, 'firstname': 'Emil', 'lastname': 'Refsnes', 'phone': 5551234, 'joined_date': datetime.date(2022, 1, 5)},
{'id': 2, 'firstname': 'Tobias', 'lastname': 'Refsnes', 'phone': None, 'joined_date': None},
{'id': 3, 'firstname': 'Linus', 'lastname': 'Refsnes', 'phone': None, 'joined_date': None},
{'id': 4, 'firstname': 'Lene', 'lastname': 'Refsnes', 'phone': None, 'joined_date': None},
{'id': 5, 'firstname': 'Stalikken', 'lastname': 'Refsnes', 'phone': None, 'joined_date': None}]>

Previous

Django Delete Data

Next chapter

Display Data

Start with Django Prepare Template

This chapter

Overview
15

Lessons

36m

Read time

1. Django Tutorial2. Django Introduction3. Django Getting Started4. Django - Create Virtual Environment5. Install Django6. Django Create Project7. Django Create App8. Django Views9. Django URLs10. Django Templates11. Django Models12. Django Insert Data13. Django Update Data14. Django Delete Data15. Django Update Model

On this page

Add Fields in the ModelInsert Data