首先需要mysql-python的包:mysqldb 通过import Mysqldb检测是否存在
在settings中配置数据库信息及应用信息:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'py', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
编写模型类:
from django.db import models
# Create your models here.
class Employee(models.Model):
name = models.CharField(max_length=20)
使用数据模型同步,可以实现orm:python manage.py syncdb 注意类名和属性的创建