python 3 django连接mysql数据库配置

来源:互联网 发布:投资网络 编辑:程序博客网 时间:2024/05/16 01:38

1 、setting

DATABASES = {
    'default': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'dbname',
    'USER':'root',
    'PASSWORD':'xxx',
    'HOST': '',
    'PORT': '',
    }

INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'app',    #新增]

}

2 、项目中的__init__.py

importpymysql
pymysql.install_as_MySQLdb() 



3 model.py


from django.db import modelsclass 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)


4 数据库迁移
python manage.py makemigrations
python manage.py migrate