[問題] django orm 讀取現有資料庫

看板Python作者 (extron)時間10年前 (2015/10/20 14:44), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/2 (看更多)
我在setting.py成功的將django連上我已經存在的MySql 在網路上看到的教學(包含官網的tutorial) 都先從create date -> save -> retrieve 但若我已經有現成的資料了 請問我怎麼透過django orm讀取資料庫? 假設我已經有一個table Fruit 並且含有很多水果的資料 於是我在 models.py 裡面 class Fruit(models.Model) name = ... color = ... 每個type都按照現有資料庫裏面Fruit的type去做宣告 也執行了 makemigrations 和 migrate all_fruit = Fruit.objects.all() print(all_fruit) 卻傳回 [] <-- 但是我資料庫裡明明有資料阿? 請問我該如何利用django orm 讀資料? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.89.32.145 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1445323492.A.3B4.html

10/20 14:55, , 1F
有沒有設定 db_table,django 的 model name 會加上 app
10/20 14:55, 1F

10/20 14:56, , 2F
名稱,可能他抓的跟你的 table 是不同的
10/20 14:56, 2F
文章代碼(AID): #1M9UBaEq (Python)
文章代碼(AID): #1M9UBaEq (Python)