Loading lesson path
To add a field to a table after it is created, open the models.py file, and make your changes:
Formula
my_tennis_club/members/models.pyfrom django.db import models
class Member(models.Model):
Formula
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
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).Formula
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.pyI will select option 2, and open the models.py file again and allow NULL values for the two new fields:
Formula
my_tennis_club/members/models.pyfrom django.db import models
class Member(models.Model):
Formula
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
members\migrations\0002_member_joined_date_member_phone.py - Add field joined_date to member - Add field phone to member
python manage.py migrate Which will result in this output:
Apply all migrations: admin, auth, contenttypes, members, sessions
Applying members.0002_member_joined_date_member_phone... OK (myworld) C:\Users\
\myworld\my_tennis_club>
We can insert data to the two new fields with the same approach as we did in the
First we enter the Python Shell: python manage.py shell Now we are in the shell, the result should be something like this:
Formula
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32Type "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
Formula
>>> 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}]>