预期的字符串或字节状对象错误

时间:2019-08-27 05:42:34

标签: python django python-3.x

当我运行migration命令时,我看到了该错误。我不知道它来自我的python文件。 我用新数据库检查模型,但不检查差异。 你有什么主意吗?它来自我的models.py吗?我的模板?我的意见?某处?

python.exe .\manage.py migrate
 Operations to perform:
 Apply all migrations: admin, auth, contenttypes, matab, sessions
Running migrations:

  Applying matab.0032_auto_20190825_1010...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 "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\migrate.py", line 234, in
handle
    fake_initial=fake_initial,
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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 "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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 "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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 "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
    field,
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 433, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 161, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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 "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1273, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1230, in to_python
    parsed = parse_date(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\dateparse.py", line 74, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

这是我的0032_auto_20190825_1010文件:

由Django 2.2.3于2019-08-25 05:40生成

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('matab', '0031_auto_20190819_1304'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='turn',
            name='Patient',
        ),
        migrations.AddField(
            model_name='turn',
            name='Date',
            field=models.DateField(default=1),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Patient_FirstName',
            field=models.CharField(default=1, max_length=100),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Patient_LastName',
            field=models.CharField(default=1, max_length=100),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Section',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='matab.Section'),
        ),
        migrations.AlterField(
            model_name='personalnfo',
            name='EducationLevel',
            field=models.CharField(blank=True, max_length=100, null=True),
        ),
        migrations.AlterField(
            model_name='turn',
            name='Doctor',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='matab.Doctor'),
        ),
        migrations.AlterField(
            model_name='turn',
            name='Time',
            field=models.TimeField(),
        ),
    ]

2 个答案:

答案 0 :(得分:3)

turn模型中,有一个字段Date,您需要为该字段提供默认日期。

尝试

from datetime import date

Date = models.DateField(default=date.today)

,如@ {shafik所述,您需要删除迁移文件,即

  

0032_auto_20190825_1010.py

答案 1 :(得分:1)

如果您的turn模型将Date的默认值设置为1,这就是引发错误的原因。

要解决此问题,请将默认值更改为timezone.now()。 为了成功运行makemigrations命令,需要删除现有的已创建迁移文件(在问题中添加)。