一 先用sqlite测试一把:
1.1 修改配置文件 server/server/setting.py
# Database # https://docs.djangoproject.com/en/dev/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
1.2 自定义model /server/blog/models.py
from django.db import models # Create your models here. class User(models.Model): name=models.CharField(max_length=20)
温馨提示:记得要Application definition (setting.py)
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', )
1.3 检查model 是否合法。
If you’re interested, also run the following commands:
- python manage.py validate – Checks for any errors in the construction of your models.
- python manage.py sqlcustom polls – Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
- python manage.py sqlclear polls – Outputs the necessary DROP TABLE statements for this app, according to which tables already exist in your database (if any).
- python manage.py sqlindexes polls – Outputs the CREATE INDEX statements for this app.
- python manage.py sqlall polls – A combination of all the SQL from the sql, sqlcustom, and sqlindexes commands.
Looking at the output of those commands can help you understand what’s actually happening under the hood.
1.4 创建数据库
python manage.py syncdb
用mysql修改下数据库就可以了。
修改配置文件 server/server/setting.py
# Database # https://docs.djangoproject.com/en/dev/ref/settings/#databases DATABASES = { 'sqlite': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), }, 'default':{ 'ENGINE': 'django.db.backends.mysql', 'NAME' : 'pytest', 'USER' : 'root', 'PASSWORD' : '1111', }, }
用MYSQL数据库实现: