想要将我现有的mysql数据库与Django一起使用 [英] Want to use my existing mysql database with django

查看:106
本文介绍了想要将我现有的mysql数据库与Django一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个django项目,现在渲染模板。我已经有一个包含表和数据负载的mysql数据库。

I have created a django project and now rendering templates. I have a mysql database already with loads of tables and data.

根据我在教程中看到的内容,python的模型概念很有趣而且很容易,但是我无法在这里使用它,因为我没有可用的模型。假设django将根据我的数据库神奇地创建模型。

From what i saw in the tutorials, the model concept of python is interesting and easy however i am not able to use it here, as i have no models available i guess. Was assuming django would magically create models based on my db.

我用引擎,数据库,用户名,主机,端口等填充了settings.py,
是否必须基于表创建模型?

I have filled up settings.py with engine, db, username, host, port etc., Do i have to create models based on my tables?

这可以工作:

db = MySQLdb.connect(user='root', db='dbBooks', passwd='1234', host='localhost')
cursor = db.cursor()
cursor.execute('SELECT bookname FROM books')
names = [row[0] for row in cursor.fetchall()]
db.close()
return render(request, 'index.html', {'bookNames': names})


推荐答案

inspectdb 现在可以正常工作。 (Django 1.7.1)只需运行manage.py inspectdb即可为数据库中的所有表创建类并显示在控制台上。

inspectdb works fine now. (Django 1.7.1) Simply running manage.py inspectdb will create classes for all tables in database and display on console.

 $ python manage.py inspectdb

使用标准Unix输出重定向将其保存为文件:

Save this as a file by using standard Unix output redirection:

 $ python manage.py inspectdb > models.py

参考

这篇关于想要将我现有的mysql数据库与Django一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆