python - 我可以在带有 Python3.x 的 Django(dev 1.6.x) 上使用 MySQL 吗?

标签 python django python-3.x

我使用来自 git repo 的 Django dev(1.6.x),我想使用 MySQL,但是在 settings.py 文件上无法设置 MySQL,因为 python3 和 Django 不支持 MySQL,所以我在 python3.x 上使用 pymysql 包没有任何问题,但在 Django 中也无法在 settings.py 上设置它。

我可以在带有 python3 的 django 上使用 mysql(或 pymysql 或?)吗?

最佳答案

我还在努力让 MySQL 与 Django 1.6 和 Python 3.3 一起工作;唯一有效的是切换到 PyMySQL。请参阅我的帖子 here

在下面添加答案

我的环境:Windows 上的 OSX 10.9、Python 3.3.3、Django 1.6.1、PyMySQL 0.6.1、MySQL Server 5.5

如何让它工作:

  1. 安装 PyMySQL 0.6.1 版本(https://github.com/PyMySQL/PyMySQL/):可以使用 pip 安装,即:pip install PyMySQL 或手动下载包;他们的网站上有一个很好的文档说明如何做到这一点。

  2. 打开您的 Django 应用 __init__.py 并粘贴以下行:

    import pymysql
    pymysql.install_as_MySQLdb() 
    
  3. 现在,打开 settings.py 并确保您的 DATABASE 属性如下所示:

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'mydb',
           'USER': 'dbuser',
           'PASSWORD': 'dbpassword',
           'HOST': 'dbhost',
           'PORT': '3306'
        }
    }
    
  4. 就是这样,你应该可以执行python manage.py syncdb来初始化你的MySQL DB;请参阅下面的示例输出:

    Creating tables ...
    Creating table django_admin_log
    Creating table auth_permission
    Creating table auth_group_permissions
    ...
    ...
    Creating table socialaccount_socialtoken
    
    You just installed Django's auth system, which means you don't have any superusers defined.
    ...
    

关于python - 我可以在带有 Python3.x 的 Django(dev 1.6.x) 上使用 MySQL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13320343/

相关文章:

Python:重新采样并向前填充到最近一个月

python - 错误 conda.core.link :_execute(502): An error occurred while installing package 'conda-forge::astor-0.7.1-py_0'

django imagefield upload_to 根本不起作用

python - Django 过滤外键字段

python - 如何复制列表中的项目(保留第一个和最后一个项目)并将列表转换为两个项目的列表列表

python - 将 Pandas Dataframe 和 numpy 数组写入通用 Excel 文件

python - 从 python 中的文件解析

python - 只能是存储在 pygame.sprite.Group 中的矩形对象

Django 'WSGIRequest' 对象没有属性 'data'

python-3.x - 发现一个值中有多少个列表