在Django中进行迁移时出现KeyError:<type =“ str>

时间:2019-01-27 04:42:58

标签: python django django-models

我创建了一个模型,并试图进行迁移,这引发了错误。

from __future__ import unicode_literals
from django.utils import timezone
from django.db import models

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length = 200)
    body = models.TextField()
    created_at = models.DateTimeField(default=timezone.now, blank = True)

这是创建模型后出现的错误。 我正在使用mysql而不是sqlite3。

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 110, in handle
    loader.check_consistent_history(connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 276, in get_new_connection
    conn.encoders[SafeBytes] = conn.encoders[bytes]
KeyError: <type 'str'>

编辑: 数据库设置 使用XAMPP mysql数据库-> localhost / phpmyadmin 这样我创建了djangoproject db

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoproject',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
    }
}

2 个答案:

答案 0 :(得分:2)

我有同样的问题。

执行此“点列表”,并检查已安装的mysqlclient版本。 如果> = 1.4;请改为这样做。 “ pip install mysqlclient == 1.3.14”。

我可以那样工作。显然1.4存在一些问题

答案 1 :(得分:0)

以常规方式备份和备份模块 MySQL客户端安装新 通过defecto pip install mysqlclient me instalo en mi entorno virtual la版本1.4 Volver a la 1.3版解决问题