一、数据库表设计(models.py文件)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
from django.db import models
#导入django用户认证表
from django.contrib.auth.models import User
# Create your models here.
class Article(models.Model):#帖子表
title = models.CharField(u"文章标题",max_length=255,unique=True)
categroy = models.ForeignKey("Category",verbose_name=u"板块")
head_img = models.ImageField(upload_to="uploads")#每个帖子都要有一张图片
content = models.TextField(u"文章内容")#帖子内容
author = models.ForeignKey("UserProfile")#一篇文章只能有一个作者
publish_date = models.DateTimeField(auto_now=True)#帖子发布日期
hidden =models.BigAutoField(default=True)#帖子是否显示
priority = models.IntegerField(u"优先级",default=1000)#帖子优先级
def __unicode__(self):#设置默认返回值
return "<%s,author:%s>" %(self.title,self.author)
class Comment(models.Model):#评论表
article = models.ForeignKey("Article")#一条评论只能针对一篇文章
user = models.ForeignKey("UserProfile")#由哪个用户发出评论
#parent_comment = models.ForeignKey(‘Comment‘,)#父级评论
parent_comment = models.ForeignKey("self", related_name = ‘parcomment‘,blank=True,null=True)#父级评论
date = models.DateTimeField(auto_now=True)#评论发布日期
comment = models.TextField(max_length=1000)#评论内容
def __unicode__(self):返回默认值
return "<%s,user:%s>" %(self.comment,self.user)
class ThumbUp(models.Model):#点赞统计
article = models.ForeignKey(‘Article‘)#点赞的文章
user = models.ForeignKey(‘UserProfile‘)#点赞的用户
date = models.DateTimeField(auto_now=True)#人点赞日期
def __unicode__(self):#设置默认返回值
return "<user:%s>" %(self.user)
class Category(models.Model):#板块表
name = models.CharField(max_length=64,unique=True)#板块名字
admin = models.ManyToManyField("UserProfile")#板块管理员多对多,多对多就是双向的一对多
def __unicode__(self):
return self.name
class UserProfile(models.Model):#用户信息表
user = models.OneToOneField(User) #继承系统原生的User表
name = models.CharField(max_length=32)
groups = models.ManyToManyField("UserGroup") #属于哪个用户组
def __unicode__(self):
return self.name
class UserGroup(models.Model):#用户组表
name = models.CharField(max_length=64,unique=True)#用户组名称
def __unicode__(self):
return self.name
二、数据初始化:
settings.py中数据库访问设置
DATABASES = {
‘default‘:{
‘ENGINE‘:‘django.db.backends.mysql‘,
‘NAME‘:‘s11bbs‘,
‘HOST‘:‘127.0.0.1‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘123456‘,
}
}
在执行同步之前要先创建数据库s11bbs,然后再执行以下语句
python manage.py migrate
mysql> use s11bbs;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_s11bbs |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
10 rows in set (0.00 sec)
python manage.py migrations
python manage.py migrate
mysql> show tables;
+----------------------------+
| Tables_in_s11bbs |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| web_article |
| web_category |
| web_category_admin |
| web_comment |
| web_thumbup |
| web_usergroup |
| web_userprofile |
| web_userprofile_groups |
+----------------------------+
18 rows in set (0.00 sec)
mysql>
三、向admin中注册建立的数据库表
在admin中注册创建的表
from django.contrib import admin
import models
# Register your models here.
admin.site.register(models.Article)
admin.site.register(models.Category)
admin.site.register(models.Comment)
admin.site.register(models.ThumbUp)
admin.site.register(models.UserProfile)
admin.site.register(models.UserGroup)
admin.site.register(models.Article)
创建用户ucode
C:\Users\ryan\PycharmProjects\s11bbs>python manage.py createsuperuser
Username (leave blank to use ‘ryan‘): ucode
Email address:
Password:
Password (again):
Superuser created successfully.
C:\Users\ryan\PycharmProjects\s11bbs>