BBS模型层搭建
目录
建表思想
配置文件
settings.py
:
python
# 默认用户模型指定
AUTH_USER_MODEL = 'User.Userinfo'
底部添加即可,用于替换默认的AbstractUser用户表
python
STATIC_URL = '/static/'
STATIC_DIRS = [
os.path.join(BASE_DIR, 'User/static')
]
创建static静态地址
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'BBS',
'USER': 'root',
'PASSWORD': '7997',
'HOST': '127.0.0.1',
'PORT': 3306,
'CHARSET': 'utf8',
}
}
更改默认数据库
模型层
User应用:
python
# models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# 用户表
class Userinfo(AbstractUser):
phone = models.CharField(verbose_name='手机号', help_text='手机号', max_length=11, null=True)
avatar = models.FileField(verbose_name='头像', help_text='头像', null=True, default='static/avatar/default.png')
register_time = models.TimeField(verbose_name='注册时间', help_text='注册时间', auto_now_add=True)
last_login_time = models.TimeField(verbose_name='最近登录时间', help_text='最近登录时间', null=True)
is_deleted = models.BooleanField(verbose_name='注销状态', help_text='注销状态', default=False)
# 一对一绑定站点表
blog = models.OneToOneField('Blog.Blog', verbose_name='关联站点', help_text='关联站点',
on_delete=models.CASCADE,related_name='blog', null=True)
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "用户表"
verbose_name_plural = verbose_name
db_table = 'Userinfo'
python
# admin.py
from django.contrib import admin
from User.models import Userinfo
# Register your models here.
# 创建一个类并用它来定制Userinfo
@admin.register(Userinfo)
# 这个类必须继承 admin.ModelAdmin
class UserinfoAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['username', 'last_name', 'email', 'phone', 'avatar', 'register_time', 'last_login_time',
'is_deleted', 'blog']
Blog应用:
python
# models.py
from django.db import models
# 个人站点表
class Blog(models.Model):
site_name = models.CharField(verbose_name='站点名', help_text='站点名', max_length=20, null=True)
site_title = models.CharField(verbose_name='站点标题', help_text='站点标题', max_length=20, null=True)
site_theme = models.CharField(verbose_name='站点样式', help_text='站点样式', max_length=255, null=True)
# 一对多绑定分类表
article_category = models.ForeignKey('Article.ArticleCategory', verbose_name='关联分类', help_text='关联分类',
on_delete=models.CASCADE, related_name='blog')
# 一对多绑定标签表
article_tags = models.ForeignKey('Article.ArticleTags', verbose_name='关联标签', help_text='关联标签',
on_delete=models.CASCADE, related_name='blog')
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "个人站点表"
verbose_name_plural = verbose_name
db_table = 'Blog'
# 广告信息表
class Adv(models.Model):
title = models.CharField(verbose_name="广告标题", help_text="广告标题", max_length=64)
content = models.TextField(verbose_name="广告详情", help_text="广告详情")
create_time = models.DateTimeField(verbose_name="创建时间", help_text="创建时间", auto_now=True)
update_time = models.DateTimeField(verbose_name="更新时间", help_text="更新时间", auto_now_add=True)
mobile = models.CharField(verbose_name="手机号", help_text="手机号", max_length=11,
default="", blank=True)
img = models.ImageField(verbose_name="广告图片", help_text="广告图片",
upload_to="static/advImg/", default="")
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "广告信息表"
verbose_name_plural = verbose_name
db_table = 'Adv'
python
# admin.py
from django.contrib import admin
from Blog.models import Blog, Adv
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['site_name', 'site_title', 'site_theme', 'article_category', 'article_tags']
@admin.register(Adv)
class AdvAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['title', 'content', 'create_time', 'update_time', 'mobile', 'img']
Article应用:
python
# models.py
from django.db import models
# 文章表
class Article(models.Model):
title = models.CharField(verbose_name='标题', help_text='标题', max_length=32, null=True)
desc = models.CharField(verbose_name='描述', help_text='描述', max_length=255, null=True)
context = models.TextField(verbose_name='内容', help_text='内容', null=True)
create_time = models.DateTimeField(verbose_name='发布时间', help_text='发布时间', auto_now_add=True, null=True)
up_num = models.BigIntegerField(verbose_name='点赞数', help_text='点赞数', null=True)
down_num = models.BigIntegerField(verbose_name='点踩数', help_text='点踩数', null=True)
comment_num = models.BigIntegerField(verbose_name='评论数', help_text='评论数', null=True)
# 多对多绑定标签表
tags = models.ManyToManyField('ArticleTags', verbose_name='评论内容', help_text='评论内容',
through='ArticleToTags', through_fields=['article', 'tag'])
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "文章表"
verbose_name_plural = verbose_name
db_table = 'article'
# 文章分类表
class ArticleCategory(models.Model):
name = models.CharField(verbose_name='分类名称', help_text='分类名称', max_length=32, null=True)
# 一对多绑定文章表
article = models.ForeignKey('Article', verbose_name='评论内容', help_text='评论内容', on_delete=models.CASCADE,
related_name='article_category', null=True)
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "文章分类表"
verbose_name_plural = verbose_name
db_table = 'article_category'
# 文章标签表
class ArticleTags(models.Model):
name = models.CharField(verbose_name='标签名称', help_text='标签名称', max_length=32, null=True)
#
# # 多对多绑定文章表
# article = models.ForeignKey('Article', verbose_name='关联文章', help_text='关联文章', on_delete=models.CASCADE,
# related_name='article_tags', null=True)
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "文章标签表"
verbose_name_plural = verbose_name
db_table = 'article_tags'
# 文章点赞点踩表
class UpDown(models.Model):
up_num = models.BigIntegerField(verbose_name='点赞数', help_text='点赞数', null=True)
down_num = models.BigIntegerField(verbose_name='点踩数', help_text='点踩数', null=True)
# 一对多绑定用户表
user = models.ForeignKey('User.Userinfo', verbose_name='关联用户', help_text='关联用户', on_delete=models.CASCADE,
related_name='up_down', null=True)
# 一对多绑定文章表
article = models.ForeignKey('Article', verbose_name='关联文章', help_text='关联文章', on_delete=models.CASCADE,
related_name='up_down', null=True)
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "文章点赞点踩表"
verbose_name_plural = verbose_name
db_table = 'up_down'
# 文章评论表
class Comment(models.Model):
content = models.TextField(verbose_name='评论内容', help_text='评论内容', null=True)
comment_time = models.DateTimeField(verbose_name='发布时间', help_text='发布时间', null=True)
# 一对多绑定用户表
user = models.ForeignKey('User.Userinfo', verbose_name='关联用户', help_text='关联用户', on_delete=models.CASCADE,
related_name='comment', null=True)
# 一对多绑定文章表
article = models.ForeignKey('Article', verbose_name='关联文章', help_text='关联文章', on_delete=models.CASCADE,
related_name='comment', null=True)
# 自关联表 表示评论的上下级关系
parent_comment = models.ForeignKey('self', verbose_name='上级评论', help_text='上级评论', on_delete=models.SET_NULL,
blank=True, null=True, related_name='son_comment')
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "文章评论表"
verbose_name_plural = verbose_name
db_table = 'comment'
# 多对多绑定文章表和标签表
class ArticleToTags(models.Model):
tag = models.ForeignKey('ArticleTags', verbose_name='关联标签', help_text='关联标签', on_delete=models.CASCADE,
related_name='article_to_tags', null=True)
article = models.ForeignKey('Article', verbose_name='关联文章', help_text='关联文章', on_delete=models.CASCADE,
related_name='article_to_tags', null=True)
class Meta:
# 指定数据库在后台显示的名字
verbose_name = "多对多绑定文章表和标签表"
verbose_name_plural = verbose_name
db_table = 'article_to_tags'
python
# admin.py
from django.contrib import admin
from Article.models import Article, ArticleCategory, ArticleTags, UpDown, Comment, ArticleToTags
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['title', 'desc', 'context', 'create_time', 'up_num', 'down_num', 'comment_num']
@admin.register(ArticleCategory)
class ArticleCategoryAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['name','article']
@admin.register(ArticleTags)
class ArticleTagsAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['name']
@admin.register(UpDown)
class UpDownAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['up_num', 'down_num', 'user', 'article']
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['content', 'comment_time', 'user', 'article', 'parent_comment']
@admin.register(ArticleToTags)
class Article_To_TagsAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['tag', 'article']
mment)
class CommentAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['content', 'comment_time', 'user', 'article', 'parent_comment']
@admin.register(ArticleToTags)
class Article_To_TagsAdmin(admin.ModelAdmin):
# 后台展示的信息,默认是全部都展示,但是可以定制展示的字段
list_display = ['tag', 'article']