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)...']

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM