【问题标题】:Configuring Django in local dev environment to work with imported MySQL database在本地开发环境中配置 Django 以使用导入的 MySQL 数据库
【发布时间】:2016-12-06 20:33:50
【问题描述】:

我通常使用 SQLite,但这个项目要求我使用来自服务器的数据,因此我没有尝试将数据从 SQLite 转换为 MySQL,而是将数据导入本地 MySQL 数据库。我想弄清楚如何配置我的 Django 项目以使用导入的数据库。现在 models.py 文件是空的。

所以我有三个问题:

  1. 我的 settings.py(如下)设置正确吗?

  2. 我是否需要设置模型以匹配导入的架构 数据库?

  3. 我还需要做什么吗?

这就是我正在使用的:

  • Django 1.8.4
  • Python 2.7
  • mysql 5.7.14
  • osx 10.11

settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

"""..."""

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'extract',
        'USER': 'root',
        'PASSWORD': 'xxxxxxxxxx',
        'HOST': 'localhost',
        'PORT': '22',
    }
}
""" ... """

【问题讨论】:

    标签: mysql django python-2.7 model localhost


    【解决方案1】:

    在settings.py中提到mysql数据库后可以试试./manage.py inspectdb。看看文档here

    【讨论】:

    • 知道了。谢谢。我是否只需将该命令的输出复制到我的 models.py 文件中?
    • 是的,你可以。还可以查看模型的Meta 部分。其内容丰富。如果您的问题得到解决,您可以将此标记为已接受的答案。 :)
    猜你喜欢
    • 2013-06-06
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2014-10-28
    相关资源
    最近更新 更多