【问题标题】:Django SYNCDB failes when adding super-user (Windows 7, mySQL, Django 1.2.4, mySQL 5.5)添加超级用户时 Django SYNCDB 失败(Windows 7、mySQL、Django 1.2.4、mySQL 5.5)
【发布时间】:2011-06-15 06:25:37
【问题描述】:

创建超级用户时syncdb失败

Django:v 1.2.4 蟒蛇:2.6 MySQL 服务器:5.5 Windows 7的 额外:MySQL-Python v1.2.3

哪些步骤会重现问题? 1.安装以上程序 2.创建项目 3. 运行同步数据库

注意:我已经安装了支持 UTF 8 的 mySQL。我还使用 CREATE DTABASE mysite_db CHARACTER SET = UTF8; 创建了 mysite_db 数据库;

预期的输出是什么?你看到了什么?

syncdb 创建所需的表如下:


C:\DjangoProjects\mysite>python manage.py syncdb
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no):

我选择“是”并收到以下错误:


Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 191,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 351,
 in handle
    return self.handle_noargs(**options)
  File "C:\Python26\lib\site-packages\django\core\management\commands\syncdb.py"
, line 103, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "C:\Python26\lib\site-packages\django\core\management\sql.py", line 182,
in emit_post_sync_signal
    interactive=interactive, db=db)
  File "C:\Python26\lib\site-packages\django\dispatch\dispatcher.py", line 172,
in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Python26\lib\site-packages\django\contrib\auth\management\__init__.py
", line 44, in create_superuser
    call_command("createsuperuser", interactive=True)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
166, in call_command
    return klass.execute(*args, **defaults)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\django\contrib\auth\management\commands\cr
eatesuperuser.py", line 71, in handle
    User.objects.get(username=default_username)
  File "C:\Python26\lib\site-packages\django\db\models\manager.py", line 132, in
 get
    return self.get_query_set().get(*args, **kwargs)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 342, in g
et
    num = len(clone)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 80, in __
len__
    self._result_cache = list(self.iterator())
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
    for row in compiler.results_iter():
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 175, in execute
    if not self._defer_warnings: self._warning_check()
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 89, in _warning_
check
    warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Incorrect string value: '\xED' for column 'username'
at row 1

您使用的是什么版本的产品?在什么操作系统上?

Django:v 1.2.4 蟒蛇:2.6 MySQL 服务器:5.5 Windows 7的 额外:MySQL-Python v1.2.3

请在下面提供任何其他信息。

我还安装了 mySQL C++ 和 ODBC 数据库连接器。 非常感谢任何帮助。

【问题讨论】:

    标签: python mysql django windows-7 syncdb


    【解决方案1】:

    看起来您在开发机器上的登录名中有一个 unicode 字符,而当 createsuperuser.py 模块查找具有该用户名的 User 对象时,这很糟糕,因为 MySQL 不期望这些字符。您可以change the collation settings on your MySQL tables 接受 UTF-8 或使用没有 unicode 字符的用户名(这不是一个很好的选择)。

    traceback中真正的失败从第71行的createsuperuser.py开始,源于第59行here

            try:
             default_username = getpass.getuser().replace(' ', '').lower()
         except (ImportError, KeyError):
             # KeyError will be raised by os.getpwuid() (called by getuser())
             # if there is no corresponding entry in the /etc/passwd file
             # (a very restricted chroot environment, for example).
             default_username = ''
    
         # Determine whether the default username is taken, so we don't display
         # it as an option.
         if default_username:
             try:
                 User.objects.get(username=default_username)
             except User.DoesNotExist:
                 pass
             else:
                 default_username = ''
    

    它通过getpass 获取您的用户名,然后尝试在User 表中查找。

    【讨论】:

    • Quib​​ble:排序规则确定排序顺序以及哪些字符比较相等。 OP 需要做的是更改数据库的 charset.
    • 谢谢你的帮助。我使用以下 SQL 重新创建了数据库: CREATE DATABASE mysite_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;并得到同样的错误。
    【解决方案2】:

    看起来您的 models.py 或夹具中的某处有 unicode 字符。将文件转换为 UTF-8 并在第一行添加注释:

    #coding-utf-8
    

    另外,在 unicode 字符串之前添加u,像这样:

    name = CharField(maxlength=255, default=u"утф-8 строка");
    

    【讨论】:

    • 谢谢。我在哪里可以找到正确的 models.py 文件? Django目录下有好几个..
    • 另外,我插入上面这段代码也是models.py吗?
    • 它是# -*- coding: utf-8 -*-
    • 嗨 AndiDog - 我应该将它插入到哪个文件中?我发现了许多“models.py”文件。谢谢。
    • @david_heagney:忘了提——我认为这不会解决你的问题。如果文件中有特殊字符,Python 解析器通常会给你一个SyntaxError(因为它默认为 ASCII)。如果您确实在某处使用特殊字符定义了用户名,并且定义了错误的编码,则必须更正该文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2011-12-18
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多