model

一 先用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:

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数据库实现:








原文地址:https://www.cnblogs.com/canbefree/p/3945687.html