columns were not added/created to my database table (postgresql) after makemigrations/migrate

40 views
Skip to first unread message

Atsunori Kaneshige

unread,
Feb 5, 2019, 9:32:52 PM2/5/19
to Django users
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

 



Nitin Kalmaste

unread,
Feb 5, 2019, 9:58:30 PM2/5/19
to django...@googlegroups.com
You need to add python's magic method in order to return something from the models you have created. Also you have to register your models to admin.py file. You can refer Django documents for that.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a1d71d4c-1500-4972-af57-dc23f5d1ff20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a1d71d4c-1500-4972-af57-dc23f5d1ff20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Atsunori Kaneshige

unread,
Feb 5, 2019, 11:16:08 PM2/5/19
to Django users
Hi Nitin,

Thank you for your comment.
I did not add any additional class in models.py.
I just added two fields under the same class, List.

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)  

In admin.py, I did not change anything.

#this is my admin.py
from django.contrib import admin
from .models import List
# Register your models here. then go to admin page and re-load
admin.site.register(List)

Why new columns (or in other words, new fields) do not show up in my admin site? Even though I did makemigrations/migrate.


Thank you for your help!

Nori
Reply all
Reply to author
Forward
0 new messages