shalin
shalin

Reputation: 373

I cannot add a datetime field in django model

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

Upvotes: 0

Views: 1445

Answers (2)

bhargavaravalik
bhargavaravalik

Reputation: 125

Delete the migration folder. And then execute

python manage.py makemigrations
python manage.py migrate

Upvotes: 1

Shahzeb Qureshi
Shahzeb Qureshi

Reputation: 612

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

Upvotes: 0

Related Questions