Adding a field to a model after initial migration

375 views
Skip to first unread message

ofeyofey

unread,
Jan 27, 2016, 2:21:16 PM1/27/16
to Django users
Hi,
I forgot to add a field to a model and wish to add it now.
I added the line to models.py but makemigrations and migrate aren't working now.
Is this possible in Django or do i need to go directly tp the sqlite DB and do it using SQL?
Thanks
Currently following this tutorial https://www.youtube.com/watch?v=g0tvJsUAx7g

sum abiut

unread,
Jan 27, 2016, 2:28:25 PM1/27/16
to django...@googlegroups.com
What error did your get? what command did you use?

try
python manage.py makemigrations
python manage.py migrate

--
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/fd23ad1b-19e9-404f-96da-df02262ab8f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Xristos Xristoou

unread,
Jan 27, 2016, 3:59:35 PM1/27/16
to Django users
no the problem is your new field affect models rules...try the new field to have null=True or default=somathing and then run on mynage.py makemigration yourapp and after migrate yourapp

christos

ofeyofey

unread,
Jan 27, 2016, 4:02:47 PM1/27/16
to Django users
Thanks for your reply.

The model.py contains,

class SignUp(models.Model):
    email
= models.EmailField()
    full_name
=  models.CharField(max_length = 120, blank = True, null = True)
    timestamp
= models.DateTimeField(auto_now_add = True, auto_now = False)
    updated
= models.DateTimeField(auto_now_add = False, auto_now = True)

   
def __unicode__(self): #__str__ for python3

         
return self.email

I had to add the updated field,

updated
= models.DateTimeField(auto_now_add = False, auto_now = True)

Running make migrations,

[pi@Huawei-HG658c-instalacion src]$ python manage.py makemigrations
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got news.views.home). Pass the callable instead.
  url
(r'^$', 'news.views.home', name='home'),

No changes detected

Then running migrations

[pi@Huawei-HG658c-instalacion src]$ python manage.py migrate
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got news.views.home). Pass the callable instead.
  url
(r'^$', 'news.views.home', name='home'),

Operations to perform:
 
Apply all migrations: admin, news, contenttypes, auth, sessions
Running migrations:
 
Rendering model states... DONE
 
Applying news.0002_signup_updated...Traceback (most recent call last):
 
File "manage.py", line 10, in <module>
    execute_from_command_line
(sys.argv)
 
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility
.execute()
 
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
   
self.fetch_command(subcommand).run_from_argv(self.argv)
 
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
   
self.execute(*args, **cmd_options)
 
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output
= self.handle(*args, **options)
 
File "/usr/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor
.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
 
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
   
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
 
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state
= self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
 
File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state
= migration.apply(state, schema_editor)
 
File "/usr/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation
.database_forwards(self.app_label, schema_editor, old_state, project_state)
 
File "/usr/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field
,
 
File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 221, in add_field
   
self._remake_table(model, create_fields=[field])
 
File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 103, in _remake_table
   
self.effective_default(field)
 
File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
   
default = field.get_db_prep_save(default, self.connection)
 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
    prepared
=False)
 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1461, in get_db_prep_value
    value
= self.get_prep_value(value)
 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1440, in get_prep_value
    value
= super(DateTimeField, self).get_prep_value(value)
 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1296, in get_prep_value
   
return self.to_python(value)
 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1399, in to_python
    parsed
= parse_datetime(value)
 
File "/usr/lib/python2.7/site-packages/django/utils/dateparse.py", line 93, in parse_datetime
    match
= datetime_re.match(value)
TypeError: expected string or buffer

Thanks,



Xristos Xristoou

unread,
Jan 27, 2016, 4:05:43 PM1/27/16
to Django users
work now ?


Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey έγραψε:

ofeyofey

unread,
Jan 27, 2016, 4:14:37 PM1/27/16
to Django users
Hi,

I changed the line to

updated = models.DateTimeField(auto_now_add = False, auto_now = True, null=True)



But the same results,

[pi@Huawei-HG658c-instalacion src]$ python manage.py makemigrations
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got news.views.home). Pass the callable instead.
  url
(r'^$', 'news.views.home', name='home'),

Migrations for 'news':
 
0003_auto_20160127_2109.py:
   
- Alter field updated on signup
[pi@Huawei-HG658c-instalacion src]$ python manage.py migrate
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got news.views.home). Pass the callable instead.
  url
(r'^$', 'news.views.home', name='home'),

Operations to perform:
 
Apply all migrations: admin, news, contenttypes, auth, sessions
Running migrations:
 
Rendering model states... DONE
 
Applying news.0002_signup_updated...Traceback (most recent call last):
 
File "manage.py", line 10, in <module>
...........

 
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1399, in to_python
    parsed
= parse_datetime(value)
 
File "/usr/lib/python2.7/site-packages/django/utils/dateparse.py", line 93, in parse_datetime
    match
= datetime_re.match(value)
TypeError: expected string or buffer




Thanks

On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:

Xristos Xristoou

unread,
Jan 27, 2016, 4:16:48 PM1/27/16
to Django users
you add your app name after the run makemigration and migrate ?if your app name is a blog then you run makemigration blog and migrate blog


Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey έγραψε:

ofeyofey

unread,
Jan 27, 2016, 4:22:01 PM1/27/16
to Django users
Ok I put the app name which is 'news' after the commands for makemigrations and for migrate.
But I am still getting the same results.


On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:

Xristos Xristoou

unread,
Jan 27, 2016, 4:24:44 PM1/27/16
to Django users
i dont know why sorry..try default value


Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey έγραψε:

ofeyofey

unread,
Jan 27, 2016, 4:29:18 PM1/27/16
to Django users
No problem. Thanks for your help.
Maybe fields can't be added to a model with django.
I will try dropping the table in the DB and see what happens.
Thanks again,


On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:

sum abiut

unread,
Jan 27, 2016, 4:35:33 PM1/27/16
to django...@googlegroups.com
You should try running it as root.

--
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.

ofeyofey

unread,
Jan 27, 2016, 5:10:50 PM1/27/16
to Django users
Alas too late... I have dropped the table.
But this link http://www.djangobook.com/en/2.0/chapter10.html
to a Django Book for version 1.0 may hold some ideas under the section about adding fields.
Thanks


On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:

Daniel Roseman

unread,
Jan 28, 2016, 4:30:47 PM1/28/16
to Django users
On Wednesday, 27 January 2016 21:29:18 UTC, ofeyofey wrote:
No problem. Thanks for your help.
Maybe fields can't be added to a model with django.
I will try dropping the table in the DB and see what happens.
Thanks again,

Adding fields to models is quite literally what migrations are for.
--
DR. 

Guan Ke

unread,
Jan 29, 2016, 9:20:19 AM1/29/16
to Django users
When you run makemigrations command, Django will compare your current model module with the newest migration module. You should have a look at your 0002_signup_updated... file to find out what cause the problem.
Reply all
Reply to author
Forward
0 new messages