标签:ima mode userinfo email dir max mail ons back
在setting里要把app的名字加进去>>installed_applications
默认为pycharm自带的sqlite,我把它改为mysql
# Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases # # DATABASES = { # ‘default‘: { # ‘ENGINE‘: ‘django.db.backends.sqlite3‘, # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), # } # } DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘mysite‘, ‘USER‘: ‘django210‘, ‘PASSWORD‘: ‘123@abAB‘, ‘HOST‘: ‘10.224.32.34‘, ‘PORT‘: ‘3306‘, } }
因为django默认使用mysqldb,python3中没有,所以要改为pymysql,不然migrate会报错。
mysite/mysite/__init__.py
import pymysql pymysql.install_as_MySQLdb()
在文件中有ORM操作指导。
from django.db import models # Create your models here. # class UserTpye(models.Model): # name = models.CharField(max_length=32) # # class UserInfo(models.Model): # username = models.CharField(max_length=32) # pwd = models.CharField(max_length=32) # email = models.CharField(max_length=32) # user_type = models.ForeignKey(UserTpye) class UserInfo(models.Model): username = models.CharField(max_length=32) pwd = models.CharField(max_length=32)
写好models后,执行迁移。
python manage.py makemigrations
在app下会出现文件,可见,额外创建ID主键。
然后执行python manage.py migrate,写入数据库。可见,表名会加上app前缀。
标签:ima mode userinfo email dir max mail ons back
原文地址:https://www.cnblogs.com/jabbok/p/9277820.html