django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟
1.配置項目setting文件
2.配置項目__init__.py 使用pymysql連接數(shù)據(jù)庫
import pymysql pymysql.version_info = (1, 20, 23) pymysql.install_as_MySQLdb()
3.在terminal中執(zhí)行語句
python manage.py inspectdb > [your app name]models.py
4.執(zhí)行遷移
python manage.py makemigrationspython manage.py migrate
5.將圖中managed = False 改成True或者刪除即可,其意義是不對數(shù)據(jù)庫進(jìn)行管理。之后修改models即可對數(shù)據(jù)庫進(jìn)行操作
知識點擴展:django,inspectdb,操作已經(jīng)存在的表
1.Django附帶了一個名為inspectdb程序,它可以通過現(xiàn)有數(shù)據(jù)庫來創(chuàng)建模型,并將相關(guān)模型代碼另存到指定文件中。在新建的newmodels.py文件中挑選指定表格對應(yīng)的模型代碼,并將其復(fù)制到相關(guān)的文件中。
python manage.py inspectdb > newmodels.py
若要操作指定數(shù)據(jù)庫,使用如下代碼:
python manage.py inspectdb --database new_schema1 > models1.py
2.默認(rèn)情況下,inspectdb創(chuàng)建非托管模型。 也就是說,在模型的Meta類中,managed = False告訴Django不要管理每個表的創(chuàng)建,修改和刪除。如果想讓Django管理表的生命周期,你需要將上面的托管選項更改為True,即將managed = False修改為managed = True。
class Person(models.Model): id = models.IntegerField(primary_key=True) first_name = models.CharField(max_length=70) class Meta: managed = False db_table = ’CENSUS_PERSONS’
3.運行migrate命令以安裝任何額外需要的數(shù)據(jù)庫記錄,如session、auth等。若對此類表格沒有操作必要,可以不用執(zhí)行下列代碼,亦可進(jìn)行數(shù)據(jù)庫操作。
python manage.py makemigrations python manage.py migrate
到此這篇關(guān)于django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟的文章就介紹到這了,更多相關(guān)django操作已有數(shù)據(jù)庫數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python安裝并操作redis實現(xiàn)流程詳解2. pip已經(jīng)安裝好第三方庫但pycharm中import時還是標(biāo)紅的解決方案3. CSS自定義滾動條樣式案例詳解4. 詳解CSS偽元素的妙用單標(biāo)簽之美5. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法6. Ajax實現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. HTML <!DOCTYPE> 標(biāo)簽8. 關(guān)于Mysql-connector-java驅(qū)動版本問題總結(jié)9. ajax post下載flask文件流以及中文文件名問題10. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
