简体   繁体   中英

django.core.exceptions.ValidationError: [u"'' value has an invalid

models.py

    # -*- coding:utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models
    # Create your models here.

    class moption(models.Model):

        pub_date = models.DateTimeField('pub time', auto_now_add=True, editable=True)

when I command "python manage.py migrate"

Operations to perform:
  Apply all migrations: Price, admin, auth, contenttypes, sessions
Running migrations:
  Applying Price.0006_moption_pub_date...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python27\lib\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 "C:\Python27\lib\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 "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\schema.py", line 43, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 395, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 147, in column_sql
    default_value = self.effective_default(field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1440, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1419, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1403, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

who can tell me,thanks,

Remove the editable=True from the pub_date ,

    pub_date = models.DateTimeField(auto_now_add=True)

Try clearing all your migrations and recreate the database,

If you are using SQLite3, then run,

rm -r Price/migrations #Assume Price is your app_name
rm -rf db.sqlite3

python manage.py makemigrations
python manage.py migrate

It seems your migration files are not properly matching with table structure in your db.

  1. If you are okay with flushing with all your db data, you can try this way,

Delete your db file(Take backup if you want)

Remove your migrations folder from each app

Create new migrations, migrate them.

Now, You have a new db file with your new table structure.

  1. If you are NOT okay with flushing with all your db data, you can try this way,

Delete all entries from your django_migrations table using sqlite console

Create new migrations

migrate them

Now, You have a new db file with your new table structure.

Deleting all your migrations, dropping your database are rather extrement suggestions and i would advice you against that. In fact, do not do either of these unless you have full data backups and al your migrations are in version control.

Right, now let's look at the signature for DateTimeField

class DateTimeField(auto_now=False, auto_now_add=False, **options)

And your usage

  pub_date = models.DateTimeField('pub time', auto_now_add=True, editable=True)

What is this 'pub time'? What ever it is, it's causing a faulty migration to be created.

After you fix your code, look at your database, see inside the django_migrations table and find the number of the last applied migration for this app. Anything in your migrations folder with a higher number is an unapplied migration. Delete only the unapplied migrations. And then delete the '*.pyc' files in the dame folder. Now do

./manage.py makemigrations
./manage.py migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM