python3 django串連mysql,同步表結構

來源:互聯網
上載者:User

標籤:roi   表結構   select   問題   記錄   pytho   tps   使用   oid   

第一步:安裝PyMySQ代替MySQLdbpip3 install PyMySQL然後在工程目錄的__init__.py中填寫下面兩句話 import pymysql
pymysql.install_as_MySQLdb()   問題:安裝過程中如果提示No module named ‘MySQLdb‘ 解決方案:python3使用上面的方法替代MySQLdb原因:python3中,不再使用MySQLdb包了第二步:設定資料庫和INSTALLED_APPS(app下的settings.py) 設定資料庫DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.mysql‘,#資料庫引擎
        ‘NAME‘: ‘UITest‘,#資料庫名
        ‘USER‘:‘root‘,#資料庫使用者名稱
        ‘PASSWORD‘:‘123456‘,#資料庫密碼
        ‘HOST‘:‘‘,#資料庫地址,預設localhost
        ‘PORT‘:‘‘,#資料庫連接埠,預設3306
    }}  配置INSTALLED_APPS在INSTALLED_APPS下添加app名INSTALLED_APPS = [    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘webManage‘,]第三步:建立model.pyclass後的是表名,繼承models.Model,下面寫表中的欄位名,欄位支援多種類型  from django.db import models
# Create your models here.

class page(models.Model):
    pageid=models.IntegerField().primary_key
    chinessname=models.CharField()
    englishname=models.CharField().unique
    pagedesc=models.CharField()

class elements(models.Model):
    elementid=models.IntegerField().primary_key
    pageid=models.ForeignKey(page)
    variablename=models.CharField().unique
    variabledesc=models.CharField()
    find_by_android_option=models.CharField()
    find_by_android_value=models.CharField()
    find_by_iOS_option=models.CharField()
    find_by_iOS_value=models.CharField() 更多關於models的使用,詳見官方文檔https://docs.djangoproject.com/en/dev/topics/db/models/ 第四步:同步資料庫檔案  檢查要修改的資料庫欄位:python3 manage.py makemigrations 執行命令後,可以看到如下會修改的內容同時會在app的migrations中產生一個000開頭的檔案,檔案內寫明了要進行的sql操作   修改資料庫:python3 manage.py migrate然後在資料庫內會看到新增的表,表名為【app名_class表示的表名】,同時在django_migrations表中添加記錄 備忘:如果settings.py中的INSTALLED_APPS沒有填寫當前app,找不到app下要同步的models檔案 

python3 django串連mysql,同步表結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.