标签:field 默认 class creat 建表 告诉 ack session 数据库名
pip install mysqlclient
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘firstsite‘, ‘HOST‘: ‘localhost‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123456‘, ‘PORT‘: ‘3306‘, } }
# 要求在自己的mysql服务中,先创建好数据库名字为:firstsite,否则django无法创建表。没有创建的话,使用python manage.py migrate,结果如下图所示:
python manage.py migrate
# migrate命令将遍历INSTALLED_APPS
设置中的所有项目,在数据库中创建对应的表,并打印出每一条动作信息
from django.db import models # Create your models here. # python manage.py makemigrations polls class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField(‘date published‘) class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
# Application definition,在配置文件settings.py中配置: INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘firstapp‘ #####################填写自己的app应用名称 ]
#当然,如果数据库中已经存在相应的表了,就不会创建相同的表了
python manage.py makemigrations firstapp
运行结果如下:
python manage.py migrate
# 默认会提供类相应的操作api
python manage.py shell
python manage.py createsuperuser
标签:field 默认 class creat 建表 告诉 ack session 数据库名
原文地址:https://www.cnblogs.com/igoodful/p/11479356.html