django整合原有的mysql数据库
虽然django适合从零开始构建一个项目,但有时候整合原有的数据库也在所难免,下面以django整合我的mysql作说明。
mysql数据是我从京东上抓取的数据,数据表名为jd,演示如图
下面将jd整合到django中,操作如下
1.修改settings.py
root@iZ28b5osxspZ:/home/jd# vim jd/settings.py ... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #'NAME': os.path.join(BASE_DIR, "jd.sql"), 'NAME':'jd', 'HOST':'127.0.0.1', 'PORT':3306, 'USER':'root', 'PASSWORD':'hehe', } } ...
2.针对已有数据库自省生成新的models
root@iZ28b5osxspZ:/home/jd# python manage.py inspectdb # This is an auto-generated Django model module. # You'll have to do the following manually to clean this up: # * Rearrange models' order # * Make sure each model has one field with primary_key=True # * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table # Feel free to rename the models, but don't rename db_table values or field names. # ▽ # You'll have to do the following manually to clean this up: # Also note: You'll have to insert the output of 'django-admin.py sqlcustom [app_label]' # into your database. from __future__ import unicode_literals from django.db import models class Jd(models.Model): ▽ id = models.IntegerField(primary_key=True) # AutoField? ▽ category = models.CharField(max_length=64, blank=True) ▽ # * Make sure each model has one field with primary_key=True name = models.CharField(max_length=128, blank=True) price = models.CharField(max_length=64, blank=True) url = models.CharField(max_length=64, blank=True) class Meta: managed = False db_table = 'jd' root@iZ28b5osxspZ:/home/jd#
3.导出模型并代替models.py
root@iZ28b5osxspZ:/home/jd# python manage.py inspectdb > models.py root@iZ28b5osxspZ:/home/jd# ls jd main manage.py models.py root@iZ28b5osxspZ:/home/jd# mv models.py main/
4.默认配置下生成不可修改/删除的models,修改meta class中的managed=True则告诉django可以对数据库进行操作
root@iZ28b5osxspZ:/home/jd# python manage.py migrate Operations to perform: Apply all migrations: admin, contenttypes, auth, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying sessions.0001_initial... OK root@iZ28b5osxspZ:/home/jd# python manage.py shell Python 2.7.6 (default, Mar 22 2014, 22:59:56) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.
5.是不是真的可以操作原有数据库了呢?进行验证即可
root@iZ28b5osxspZ:/home/jd# python manage.py shell Python 2.7.6 (default, Mar 22 2014, 22:59:56) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.
In [1]: from main.models import Jd In [2]: Jd.objects.all() Out[2]: [<Jd: 雅克菱正畸保持器清洁片 3g*24片>, <Jd: 洁灵中药漱口水 320ml(清除异味 清新口气)冬青薄荷型>, <Jd: 静佳JPlus 羽泉三秒钟酷感口腔喷雾 清新口气>, <Jd: 雅克菱假牙清洁片60片 薄荷味口气清新剂>, <Jd: 澳多-C(Victoria-C) 漱口水冰蓝超爽口味600ml>, <Jd: 保丽净假牙清洁片局部假牙专用30片>, <Jd: 李施德林漱口水500ml*3(天然橙味 清凉口味 冰蓝口味)>, <Jd: saky舒客舒克专业清新漱口水天然鲜橙380ml送120ml去口臭不含氟>, <Jd: 保丽净全/半口假牙专用假牙清洁片30片>, <Jd: 李施德林漱口水 天然橙味250ml*5 防蛀牙固齿去除口臭口气 刺激性小>, <Jd: saky舒客舒克漱口水清凉薄荷380ml送120ml 去口臭>, <Jd: 保丽净假牙全口清洁片60片 24片 加送牙盒牙刷>, <Jd: 保丽净假牙护理套装(清洁片60片) 舒适达专业修复牙膏100g>, <Jd: LION狮王 ETIQUETTE清新口喷5ml*2支 清凉薄荷>, <Jd: 皓齿健(Hosjam) 芨效抗敏漱口水500ml>, <Jd: 好易康生物酶漱口水 去除口气口臭牙龈出血牙周炎 杀菌防蛀 薄荷味 250ML>, <Jd: 保丽净假牙清洁片30片*2盒>, <Jd: 香港版 李施德林 淡香草 漱口水1000ml 除口臭 清新口气 去牙渍>, <Jd: 除口臭漱 2瓶装 口水液喷剂口气清新剂口喷雾口腔溃疡喷雾剂口苦口干>, <Jd: 日本狮王(Lion) ETIQUETTE清新口喷(清凉薄荷) 5ml 原装进口>, '...(remaining elements truncated)...']