013-Django Update Model

Objectives: Django Update Model

Django Update Model

Updating the Model in Django

1. Add Fields to the Model

Open models.py and update your Member model:

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)

Real-life Example:

This is like adding a phone number and join date columns in a student register after the class has already started.

2. Make Migrations

Create migrations to update the database:

python manage.py makemigrations members

If Django asks about non-nullable fields, either provide a default or allow NULL values in models.py.

3. Apply Migrations

python manage.py migrate

This applies the changes and updates your database table.

4. Insert Data into New Fields

Open the Python shell and update a record:

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

5. Verify Update

>>> Member.objects.all().values()
<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}
]>

Advice:

Always allow NULL values for new fields if your table already contains records, otherwise migrations will fail.

Exercises (10 Q&A)

  1. Question: How do you add a new field 'email' to the Member model?
    Answer: email = models.CharField(max_length=255, null=True)
  2. Question: What command creates a migration for a specific app?
    Answer: python manage.py makemigrations members
  3. Question: How do you apply migrations to the database?
    Answer: python manage.py migrate
  4. Question: How do you open the Python shell to update data?
    Answer: python manage.py shell
  5. Question: How do you select the first record in the Python shell?
    Answer: x = Member.objects.all()[0]
  6. Question: How do you add a phone number to the first member?
    Answer: x.phone = 5551234
    x.save()
  7. Question: How do you add a join date to the first member?
    Answer: x.joined_date = '2022-01-05'
    x.save()
  8. Question: How do you see all records after updating the model?
    Answer: Member.objects.all().values()
  9. Question: Why should new fields allow NULL for existing records?
    Answer: To prevent migration errors when the table already has data.
  10. Question: What happens if you try to add a non-nullable field without default?
    Answer: Django will prompt you to provide a default or stop the migration.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::