【问题标题】:I have a TypeError: expected string or bytes-like object during "migrate" (Django)我有一个 TypeError: expected string or bytes-like object during "migrate" (Django)
【发布时间】:2018-05-29 09:35:30
【问题描述】:

我正在使用 Django。我更改了模型中的某些内容,即“日期字段”。 我将它从 datetimefield ('taxi_time') 更改为 datefield ('taxi_date') & timefield ('taxi_time')。我做了manage.py makemigrations,但我不能manage migrate

错误如下。

  Running migrations:
  Applying taxi.0017_auto_20171214_2256... OK
  Applying taxi.0018_auto_20171214_2336...Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/heesu/myvenv/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 "/home/heesu/myvenv/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 "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/heesu/myvenv/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 "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 87, in database_forwards
    field,
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 238, in add_field
    self._remake_table(model, create_field=field)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
    self.effective_default(create_field)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 229, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1301, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1296, in get_prep_value
    return self.to_python(value)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1258, in to_python
    parsed = parse_date(value)
  File "/home/heesu/myvenv/lib/python3.5/site-packages/django/utils/dateparse.py", line 61, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

我尝试将 datefield 更改为 charfield,并且我还删除了所有现有模型以避免损坏。但我仍然得到这个错误。我猜这与日期字段或时间字段有关,但我不知道发生了什么。 什么问题???

以下是我的模型

class Recruit(models.Model):
taxi_from = models.CharField('출발지', max_length=20)
taxi_from_detail = models.CharField(max_length=50)
taxi_to = models.CharField('도착지', max_length=20)
taxi_to_detail = models.CharField(max_length=50)
taxi_date = models.DateField('출발 날짜')
taxi_time = models.TimeField('출발시각')
taxi_flex = models.BooleanField('시간 변경 가능', default=0)
taxi_popnow = models.PositiveIntegerField('현재 인원', default=1)
taxi_poptot = models.PositiveIntegerField('택시 총원', default=4
taxi_is_closed = models.BooleanField('마감', default=False)
host_id = models.CharField(max_length=150)
host_name = models.CharField('올린이', max_length=50, default="user")
host_cont = models.CharField('연락처', max_length=100, default="062-715-0000")
host_ref = models.CharField('참고사항', max_length=200, default="Depart after 1 hr")
create_time = models.DateTimeField('time created')

def __str__(self):
    return self.host_name

def was_created_recently(self):
    now = timezone.now()
    return timezone.now() - datetime.timedelta(days=1) <= self.create_time <= now

def chk_closed(self):
    now = datetime.datetime.now()
    taxi_datetime = datetime.datetime.combine(self.taxi_date, self.taxi_time)
    is_full = self.taxi_poptot <= self.taxi_popnow
    is_past = taxi_datetime <= now
    if (is_full or is_past):
        self.taxi_is_closed = True
    else:
        self.taxi_is_closed = False
    self.save()

def force_closed(self):
    self.taxi_is_closed = True
    self.host_id = "admin"
    self.save()

编辑 我从manage.py sqlmigrate taxi 0032_auto_20171215_1619 获得了关注,这是我的最后一次迁移

System check identified some issues:

WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_CONTEXT_PROCESSORS.
BEGIN;
--
-- Alter field taxi_date on recruit
--
ALTER TABLE "taxi_recruit" RENAME TO "taxi_recruit__old";
CREATE TABLE "taxi_recruit" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "taxi_date" date NOT NULL, "taxi_from" varchar(20) NOT NULL, "taxi_to" varchar(20) NOT NULL, "taxi_flex" bool NOT NULL, "taxi_popnow" integer unsigned NOT NULL, "taxi_poptot" integer unsigned NOT NULL, "host_name" varchar(50) NOT NULL, "host_cont" varchar(100) NOT NULL, "host_ref" varchar(200) NOT NULL, "create_time" datetime NOT NULL, "taxi_from_detail" varchar(50) NOT NULL, "taxi_to_detail" varchar(50) NOT NULL, "host_id" varchar(150) NOT NULL, "taxi_is_closed" bool NOT NULL, "taxi_time" integer NULL);
INSERT INTO "taxi_recruit" ("id", "taxi_is_closed", "taxi_flex", "taxi_to", "taxi_poptot", "taxi_from", "taxi_time", "host_id", "host_ref", "taxi_from_detail", "host_cont", "taxi_popnow", "create_time", "host_name", "taxi_date", "taxi_to_detail") SELECT "id", "taxi_is_closed", "taxi_flex", "taxi_to", "taxi_poptot", "taxi_from", "taxi_time", "host_id", "host_ref", "taxi_from_detail", "host_cont", "taxi_popnow", "create_time", "host_name", coalesce("taxi_date", NULL), "taxi_to_detail" FROM "taxi_recruit__old";
DROP TABLE "taxi_recruit__old";
--
-- Alter field taxi_time on recruit
--
ALTER TABLE "taxi_recruit" RENAME TO "taxi_recruit__old";
CREATE TABLE "taxi_recruit" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "taxi_from" varchar(20) NOT NULL, "taxi_to" varchar(20) NOT NULL, "taxi_flex" bool NOT NULL, "taxi_popnow" integer unsigned NOT NULL, "taxi_poptot" integer unsigned NOT NULL, "host_name" varchar(50) NOT NULL, "host_cont" varchar(100) NOT NULL, "host_ref" varchar(200) NOT NULL, "create_time" datetime NOT NULL, "taxi_from_detail" varchar(50) NOT NULL, "taxi_to_detail" varchar(50) NOT NULL, "host_id" varchar(150) NOT NULL, "taxi_is_closed" bool NOT NULL, "taxi_date" date NOT NULL, "taxi_time" time NOT NULL);
INSERT INTO "taxi_recruit" ("id", "taxi_is_closed", "taxi_flex", "taxi_to", "taxi_poptot", "taxi_from", "taxi_time", "host_id", "host_ref", "taxi_from_detail", "host_cont", "taxi_popnow", "create_time", "host_name", "taxi_date", "taxi_to_detail") SELECT "id", "taxi_is_closed", "taxi_flex", "taxi_to", "taxi_poptot", "taxi_from", coalesce("taxi_time", NULL), "host_id", "host_ref", "taxi_from_detail", "host_cont", "taxi_popnow", "create_time", "host_name", "taxi_date", "taxi_to_detail" FROM "taxi_recruit__old";
DROP TABLE "taxi_recruit__old";
COMMIT;

【问题讨论】:

  • Django 分三步进行迁移,请分享第二步结果和taxi.0018_auto_20171214_2336 文件内容。我认为这个问题是因为删除和添加新字段的冲突。
  • @NamNguyễn 如何与您分享?我的意思是,我怎样才能打开那个文件?
  • 我使用了manage.py sqlmigrate taxi 0018_auto_20171214_2336,但我得到错误为` .... File "/home/heesu/myvenv/lib/python3.5/site-packages/django/db/models/fields /__init__.py",第 1258 行,在 to_python 中解析 = parse_date(value) 文件 "/home/heesu/myvenv/lib/python3.5/site-packages/django/utils/dateparse.py",第 61 行,在 parse_date match = date_re.match(value) TypeError: expected string or bytes-like object`
  • 我解决了问题!我从@NamNguyễn 和link 那里得到了提示!谢谢!

标签: python django model typeerror migrate


【解决方案1】:

删除您最近的迁移, 然后在创建一个迁移后添加这个

from django.utils import timezone

date_created = models.DateTimeField('date_created', default=timezone.now(), blank=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2019-11-11
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多