我有一个TypeError:“迁移”期间预期的字符串或类似字节的对象(Django)

时间:2017-12-15 07:44:56

标签: python django model typeerror migrate

我正在使用Django。我在模型中改变了一些东西,即'datefield'。 我把它从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,并且还删除了所有现有模型以避免损坏。但我还是得到了这个错误。我想这与datefield或timefield有关,但我不知道发生了什么。 有什么问题???

以下是我的模特

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;

1 个答案:

答案 0 :(得分:0)

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

from django.utils import timezone

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