I've added an IntegerField to a Django model, but I just can't migrate it.
I type:
python3 migrate my_app
and it tells me that there are no migrations to apply. Then, of course, my admin page outputs an error because it can't find the new column! Any ideas?
Thanks
Edit: console output
Rendering model states... DONE
Applying jokes.0003_joke_no...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 702, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 1868, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''
Related
I'm trying to override the save method and I think I write exactly what the document said, but get the error
from django.db import models
class Post(models.Model):
name = models.CharField(max_length = 20)
lower_name = models.CharField(max_length = 20)
def save(self, *args, **kwargs):
if not self.lower_name:
self.lower_name = self.name.lower()
super().save(*args, **kwargs)
here is the traceback when I call python manage.py migrate
(project_site) λ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, works
Running migrations:
Applying works.0018_post_url_name...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
field,
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 327, in add_field
self._remake_table(model, create_field=field)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 188, in _remake_table
self.effective_default(create_field)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\backends\base\schema.py", line 212, in _effective_default
default = field.get_default()
File "D:\Code\Python\project_site\.venv\lib\site-packages\django\db\models\fields\__init__.py", line 797, in get_default
return self._get_default()
TypeError: save() missing 1 required positional argument: 'self'
python version: 3.6.8 django version: 2.2.4
I've deployed my Django application to DigitalOcean server via nginx, gunicorn. Today I wanted to add the script models.DateTimeField(auto_now_add=True) for saving the date when message from user was sent, but during migration (after pulling the entire repository from github on ubuntu server) I'm getting following error. And I guess the problem is in migration files, but don't know how can I manipulate them on server and how to do it safely, without losing any data.
Error:
Applying pragmatechapp.0004_auto_20200328_1036...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 793, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1435, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1414, in get_prep_value
value = super().get_prep_value(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1274, in get_prep_value
return self.to_python(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1375, in to_python
parsed = parse_datetime(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/utils/dateparse.py", line 106, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
Note: I should solve this problem very quickly, please, everyone, who knows the answer, help me. I'm really depressed and stressed.
As a solution I've used deleting the migrations one by one with a command find . -path "*/migrations/{my_migration_names}.py" -not -name "__init__.py" -delete. To get name of all of my migration files you should run python3 manage.py showmigrations command.
I have a projects with hundreds of migrations. When I try to run the tests it gives me the stacktrace:
./manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/project/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "USING": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/project/lib/python3.5/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/site-packages/django/core/management/commands/test.py", line 62, in handle
failures = test_runner.run_tests(test_labels)
File "/project/lib/python3.5/site-packages/django/test/runner.py", line 601, in run_tests
old_config = self.setup_databases()
File "/project/lib/python3.5/site-packages/django/test/runner.py", line 546, in setup_databases
self.parallel, **kwargs
File "/project/lib/python3.5/site-packages/django/test/utils.py", line 187, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "/project/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 69, in create_test_db
run_syncdb=True,
File "/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/project/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/site-packages/wagtail_modeltranslation/management/commands/migrate_translation.py", line 25, in handle
super(Command, self).handle(*args, **options)
File "/project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/project/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/project/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 108, in database_forwards
self._run_sql(schema_editor, self.sql)
File "/project/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 133, in _run_sql
schema_editor.execute(statement, params=None)
File "/project/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 136, in execute
cursor.execute(sql, params)
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/project/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/project/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/project/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/project/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "USING": syntax error
From this alone I cannot seem to figure out the cause (which probably is a migration somewhere).
When I try to run the same command using a postgres database, I get a error
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/test.py", line 29, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/test.py", line 62, in handle
failures = test_runner.run_tests(test_labels)
File "/project/lib/python3.5/dist-packages/django/test/runner.py", line 601, in run_tests
old_config = self.setup_databases()
File "/project/lib/python3.5/dist-packages/django/test/runner.py", line 546, in setup_databases
self.parallel, **kwargs
File "/project/lib/python3.5/dist-packages/django/test/utils.py", line 187, in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
File "/project/lib/python3.5/dist-packages/django/db/backends/base/creation.py", line 69, in create_test_db
run_syncdb=True,
File "/project/lib/python3.5/dist-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/project/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/project/lib/python3.5/dist-packages/wagtail_modeltranslation/management/commands/migrate_translation.py", line 25, in handle
super(Command, self).handle(*args, **options)
File "/project/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/project/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/project/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/project/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 221, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 531, in alter_field
old_db_params, new_db_params, strict)
File "/project/lib/python3.5/dist-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
new_db_params, strict,
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 635, in _alter_field
new_default = self.effective_default(new_field)
File "/project/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 245, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
prepared=False)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
value = self.get_prep_value(value)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1043, in get_prep_value
return self.to_python(value)
File "/project/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1036, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'N' value must be either True or False."]
Since I use the value N all over the project, this does not help much either.
How do I go about finding the migration that causes these errors?
You can add the --verbosity of -v option when running your tests, which adds more verbosity to the output of your tests:
python manage.py test -v 2
or
python manage.py test --verbosity=2
Level 2 should show you all the migrations. Level 3 even shows more details, but I don't think it'll add much in your case.
This will show you which migration is causing the issue.
Ran my server today and realized I had 'two unapplied migrations'. I ran the command makemigrations and it detected no change, so naturally I then ran migrate.
I get this error code on my terminal:
Applying Campaigns.0016_campaignrecruit_date...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 371, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", l
ine 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line
335, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrat
e.py", line 200, in handle
fake_initial=fake_initial,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", lin
e 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/migration.py", li
ne 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/operations/fields
.py", line 84, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
line 306, in add_field
self._remake_table(model, create_field=field)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py",
line 178, in _remake_table
self.effective_default(create_field)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/schema.py", li
ne 240, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1276, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1271, in get_prep_value
return self.to_python(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 1233, in to_python
parsed = parse_date(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/dateparse.py", line 74, i
n parse_date
match = date_re.match(value)
TypeError: expected string or bytes-like object
So to me, it looks like a date field in my model campaignrecruit is getting an input that it doesn't know how to handle. Which I would understand. But the thing is I don't have a datefield in my campaignrecruit model. The Time field is intentionally commented out right now.
Here's my models.py:
class campaignrecruit(models.Model):
campaign = models.ForeignKey(startcampaign, on_delete=models.CASCADE)
volunteer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
join = models.BooleanField()
#Time = models.IntegerField()
def __str__(self):
return str(self.volunteer)
What else would throw this error? Any and all help is appreciated.
Since no one else answered, I will. As Aritra stated, I totally forgot about the migration files that are created whenever you run the migrate command. I had removed these model fields from my models and whenever I migrated again there was an error removing the files in my migrations file. After manually going in and removing the files, my error was fixed.
The database was just out of sync with what django had in its files.
Models.py
class user_Roles(models.Model):
code = models.CharField(max_length=20)
description = models.CharField(max_length=30)
permitted_menus = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True,blank=True,null=True)
I cannot add a datetime, date or time fields in models. It shows that "[u"'' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]" while migrating the database. How to fix it??
Error
Running migrations:
Rendering model states... DONE
Applying myapp.0025_auto_20160321_0913...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 567, in _alter_field
new_default = self.effective_default(new_field)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2293, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2288, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2275, in to_python
params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]
After removing the created field also, it shows the same error..
Delete the migration folder. And then execute
python manage.py makemigrations
python manage.py migrate
You can modify the created field like this
created = models.DateTimeField(default=date.today, blank=True,null=True)
Check the documentation for auto_now_add