Django迁移错误:TypeError期望字符串或类字节对象

时间:2016-11-01 03:15:37

标签: python django

我试图学习django并且在更改模型时会出现错误。我尝试了很多像default = datetime.datetime.now但我不知道如何解决它。

这些是我的模特

来自django.db导入模型 导入日期时间

class Candidate(models.Model):
    name = models.CharField(max_length=10)
    introduction = models.TextField()
    area = models.CharField(max_length=15)
    party_number=models.IntegerField(default=0)
    def __str__(self) :
        return self.name

class Poll(models.Model) :
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    area = models.CharField(max_length=15)

class Choice(models.Model) :
    poll = models.ForeignKey(Poll)
    candidate = models.ForeignKey(Candidate)
    votes = models.IntegerField(default=0)

当我输入commmand:python manage.py migrate时,发生此错误

Operations to perform:
  Apply all migrations: admin, ang, auth, contenttypes, sessions
Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python\Python35-32\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:\Python\Python35-32\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:\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python\Python35-32\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:\Python\Python35-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
    self.effective_default(field)
  File "C:\Python\Python35-32\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:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1438, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1417, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1378, in to_python
    parsed = parse_datetime(value)
  File "C:\Python\Python35-32\lib\site-packages\django\utils\dateparse.py", line 93, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

请帮助我!!

12 个答案:

答案 0 :(得分:5)

如果您修改了模型中的字段。之后你运行makemigrations,那时候就像这样问

^C(api_env)nyros@nyros:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1

我们选择1个选项,然后它会显示如下

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

我们给了timezone.now()然后makemigrations完成。

查看迁移的最后一个文件,有

class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('providers', '0023_remove_content_provider'),
]

operations = [
    migrations.AddField(
        model_name='content',
        name='provider',
        field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
        preserve_default=False,
    ),
]

在上面的代码中观察这一行

default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)

在该行中,forgeinkey字段默认值为datetime,是否为正确值?

不,所以你必须给一些字符串或对象是该字段的默认值。

现在您必须在相应的迁移文件中编辑该值,如此

default='some string'

然后保存并运行migrate命令。

试着让我知道,它是否有效。谢谢

答案 1 :(得分:3)

使用以下命令删除迁移:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

然后使用rm db.sqlite

删除数据库文件

然后运行您的服务器

答案 2 :(得分:2)

更改auto_now_add=True

的模型字段默认值
end_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(auto_now_add=True)

答案 3 :(得分:0)

只需删除迁移文件夹中的所有文件,但&#34; init.py&#34; 并删除&#34; db.sqlite3&#34;即您的数据库,现在进行所需的更改并进行迁移。希望这有助于:)

答案 4 :(得分:0)

这里同样的问题,当通过向我的日期字段添加占位符时,我不小心输入了#34; 1&#34;在哪里&#34; django.utils.timezone.now&#34;应该是,这是日期时间的默认值。进入上次迁移,找到&#34; default = xxx&#34;这与其他人不一样,并插入&#34; django.utils.timezone.now&#34;

答案 5 :(得分:0)

转到迁移文件夹“ migrations

转到您的迁移文件,例如"0054_comment_date.py"

更改'default'行中的field=models.DateTimeField(auto_now_add=True, default=0)

从0或其他任何数字

default='2012-09-04 06:00:00.000000-08:00'

答案 6 :(得分:0)

我遇到了同样的问题。解决方案是删除“ migrations”文件夹中的所有默认空格。

在您键入“ python manage.py migration”时,在终端中向上查找,找到“ migrations”中的确切文件停止迁移的位置。然后找到DateField或DateTimeField的所有默认参数并将其删除。这些模型无法使用默认参数处理。

migrations.AlterField(
        model_name='order',
        name='start_date',
        field=models.DateField(auto_now_add=True, ***BUG default=False BUG***),
        preserve_default=False,
    ),

查看终端中的日志:

Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):

此文件中有错误。去那里,从DateField删除默认参数

答案 7 :(得分:0)

作为解决方法,可以运行./manage.py migration --fake

答案 8 :(得分:0)

如果上述所有解决方案都不适合您,例如我的情况。您可以通过以下简单步骤轻松解决该问题。

  1. 进入您的应用目录,并删除所有以前的迁移,仅留下

pycache 初始化 .py

  1. 进入主项目目录并删除数据库。 (注意:没有副作用,请随便做吧)

  2. 完成所有操作,打开您的终端并运行 $ python manage.py makemigrations $ python manage.py migration

完成。谢谢。

答案 9 :(得分:0)

您可以只使用 end_date = models.DateTimeField(auto_now_add=True),它会自动为您创建一个默认日期。 如果此错误无法解决,请尝试删除所有迁移。之后再次使用python manage.py makemigrationspython manage.py makemigrations <app_name>,然后迁移,它肯定可以工作。

我遵循了这个,这对我有用

答案 10 :(得分:0)

手动删除所有迁移。然后输入 python manage.py makemigrationspython manage.py migrate

-- 希望这可能有用.. 对我有用。

答案 11 :(得分:-2)

我遇到了同样的问题。查看调用makemigrations后生成的.py文件中的所有models.DateTimeField行。在那里你会注意到一些DateTimeField有一个错误的默认值。例如,用models.DateTimeField(auto_now_add = True,default = django.utils.timezone.now)替换它们。