Hi, Django masters!
My app has simple models.py
just two fields like below.
#models.py
class List(models.Model):
item = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
#migration 0001
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='List',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('item', models.CharField(max_length=200)),
('completed', models.BooleanField(default=False)),
],
),
]
and after makemigrations/migrate, the app was working no problem.
Then, I wanted to try adding two more fields.
#new models.py
class List(models.Model):
item = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
created_at = models.DateTimeField('date', auto_now_add=True, null = True)
updated_at = models.DateTimeField('update', auto_now=True, null = True)
#migrations 0002
class Migration(migrations.Migration):
dependencies = [
('pages', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='list',
name='created_at',
field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='date'),
),
migrations.AddField(
model_name='list',
name='updated_at',
field=models.DateTimeField(auto_now=True, null=True, verbose_name='update'),
),
]
Django doc says that I need to add 'null = True' because I am using postgresql as my database.
Without 'null = True', the program throws an error saying that column pages_list.created_at doesn't exist etc. when I try to see the web page.
But, with 'null = True', I successfully did makemigration and migrate, then web page showed up without any problem.
But, when I go to admin, I could not find any fields that I though I created.
I also looked at the table in my postgresql, but again, there are only original columns, but not other two columns.
Do you know how to add new columns by changing models.py??
What should I take care to successfully modify models and add new columns in my database table (postgresql in my case).
I really appreciate your advice!
Looking forward to hearing from you.
Nori