1 配置数据库 100行左右
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'students',
'NAME': 'django_mall',
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
2 加载模块
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'mall.apps.MallConfig', #商品模块
'accounts.apps.AccountsConfig', #用户账户模块
'system.apps.SystemConfig', #系统模块
'mine.apps.MineConfig', #个人模块
'weibo.apps.WeiboConfig',
'grade.apps.GradeConfig'
]
3 创建模型数据库
constants.py
# 系统模块-轮播图配置
SLIDER_TYPE_INDEX = 11
SLIDER_TYPES_CHOICES = (
(SLIDER_TYPE_INDEX,'首页')
)
#系统模块 - 新闻通知
NEWS_TYPE_NEW = 11
NEWS_TYPE_NOTICE = 12
NEWS_TYPES_CHOICES = (
(NEWS_TYPE_NEW,'新闻'),
(NEWS_TYPE_NOTICE,'通知')
)
from django.db import models
from utils import constants
# Create your models here.
class Slider(models.Model):
""" 系统轮播图 """
name = models.CharField('名称',max_length=32)
desc = models.CharField('描述',max_length=100,null=True,blank=True)
type = models.SmallIntegerField('展现位置',
choices=constants.SLIDER_TYPES_CHOICES,
default=constants.SLIDER_TYPE_INDEX)
img = models.ImageField('图片', upload_to='slider')
reorder = models.SmallIntegerField('排序',default=0,help_text='数字越大,越靠前')
start_time = models.DateTimeField('生效开始时间',null=True,blank=True)
end_time = models.DateTimeField('生效结束时间',null=True,blank=True)
target_url = models.CharField('跳转地址',max_length=255,null=True,blank=True)
is_valid = models.BooleanField('是否删除',default=True)
created_at = models.DateTimeField('创建时间',auto_now_add=True)
updated_at = models.DateTimeField('最后修改时间',auto_now=True)
class Meta:
db_table = 'system_slider'
ordering = ['-reorder']
class News(models.Model):
""" 新闻及通知 """
types = models.SmallIntegerField('类型',choices=constants.NEWS_TYPES_CHOICES,
default=constants.NEWS_TYPE_NEW)
title = models.CharField('标题',max_length=255)
content = models.TextField('内容')
reorder = models.SmallIntegerField('排序', default=0, help_text='数字越大,越靠前')
start_time = models.DateTimeField('生效开始时间', null=True, blank=True)
end_time = models.DateTimeField('生效结束时间', null=True, blank=True)
view_count = models.IntegerField('浏览次数',default=0)
is_top = models.BooleanField('是否置顶',default=False)
is_valid = models.BooleanField('是否删除', default=True)
created_at = models.DateTimeField('创建时间', auto_now_add=True)
updated_at = models.DateTimeField('最后修改时间', auto_now=True)
class Meta:
db_table = 'system_news'
ordering = ['-reorder']
4 命令同步数据表
python manage.py check
python manage.py makemigrations
python manage.py migrate
同步成功