Setting 配置:
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'NAME': 's3',
- 'USER': 'root',
- 'PASSWORD': '123456',
- 'HOST': '127.0.0.1',
- 'PORT': '3306',
- }
- }
默认 python 使用的 MysqlDB 连接, Python3 支持支持 pymysql 所有需要在 app 里面的__init__加上下面配置:
- import pymysql
- pymysql.install_as_MySQLdb()
models 是用类的方式来配置管理数据库表的 需要继承 models.Model 这个类来管理数据库:
- class ec2(models.Model):
- name = models.CharField(max_length=32)
- price = models.IntegerField()
- pub = models.CharField(max_length=22)
数据库表建立好后需要让表写入到数据库
- python manage.py makemigrations
- python manage.py migrate
- Please select a fix:
- 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) #现在提供一次性默认值 (将在所有现有行上设置此列的空值)
- Select an option: 1 #选择 1
- Please enter the default value now, as valid Python
- The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
- Type 'exit' to exit this prompt
- >>> "pool" #添加默认字段内容
来源: http://www.bubuko.com/infodetail-2672964.html