Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實例
如果使用默認的數(shù)據(jù)庫 SQLite3,則無需配置settings.py
使用其他數(shù)據(jù)庫,則需要配置settings.py,這里以Mysql為例;
DATABASES = { 'default': { 'ENGINE':'django.db.backends.mysql', 'NAME':'webapp',#數(shù)據(jù)庫名 'USER':'test1',#用戶名 'PASSWORD':'123456',#密碼 'HOST':'127.0.0.1', 'PORT':'3306', } }
在models.py中完成數(shù)據(jù)模型的創(chuàng)建:
class student(models.Model): # class Meta: # db_table = 'User_table'#指定數(shù)據(jù)表的名稱 name = models.CharField(max_length=50) sex = models.CharField(max_length=10) birthday = models.DateField() telephone = models.BigIntegerField() def __str__(self): return self.name
并且在admin.py中完成注冊:
from webapp import models admin.site.register(models.student)
最后 cd 進入 manage.py 所在的那個文件夾下,輸入下面的命令
# Django 1.6.x 及以下 python manage.py syncdb # Django 1.7 及以上的版本需要用以下命令 python manage.py makemigrations python manage.py migrate #python2.7 manage.py **** #如果安裝多個python版本的話,最好指定版本
如果執(zhí)行python manage.py migrate報錯,嘗試python2.7 manage.py migrate appname --fake命令。
如果還不行,檢查有沒有錯誤,再來一遍。
以上這篇Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python使用Matplotlib繪圖及設置實例(用python制圖)
Python matplotlib包可以畫各種類型的圖,功能非常齊全,下面這篇文章主要給大家介紹了關于python使用Matplotlib繪圖及設置的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05Python plt.imshow函數(shù)及其參數(shù)使用
plt.imshow()是Matplotlib庫中的一個函數(shù),主要用于顯示圖像或矩陣數(shù)據(jù),本文主要介紹了Python plt.imshow函數(shù)及其參數(shù)使用,具有一定的參考價值,感興趣的可以了解一下2024-02-02python的數(shù)據(jù)與matlab互通問題:SciPy
這篇文章主要介紹了python的數(shù)據(jù)與matlab互通問題SciPy,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn)
這篇文章主要介紹了Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02