from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser
'''
bbs表结构总结:
- DateTimeField - auto_now_add = True :当记录创建时自动添加当前时间
- FileField - upload_to = "":上传文件的保存路径;默认使用头像文件;default:默认文件
- TextField : 大文本字段,可存储大量数据内容
- ForeignKey(to='self', to_field='nid', null=True) :与自身表的nid字段进行自关联
- class Meta:unique_together = (('user', 'article'),):
实现表内俩个字段的联合唯一,防止数据的赃污
'''
class UserInfo(AbstractUser):
'''
UserInfo - 用户表(继承AbstractUser)
-nid
-name
-password
-email
-phone
-avatar : 用户头像
-create_date : 用户注册时间
-blog : 对应博客表,一对一
'''
nid = models.AutoField(primary_key=True)
phone = models.CharField(max_length=32, null=True)
# upload_to = "" 上传文件的保存路径;默认使用头像文件
avatar = models.FileField(upload_to='avatar/', default='/static/img/default.png')
# auto_now_add 当记录创建时自动添加当前时间
create_date = models.DateTimeField(auto_now_add=True)
blog = models.OneToOneField(to='Blog', to_field='nid',null=True)
class Blog(models.Model):
'''
Blog - 博客表
-nid
-title :博客标题,座右铭
-site_name : 站点名字
-theme :博客主题样式,可存储css文件
'''
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=64)
site_name = models.CharField(max_length=32)
theme = models.CharField(max_length=54)
class Category(models.Model):
'''
Category - 文章分类表
-nid
-title :文章分类标题
-blog :对应博客表,一对多关系,一个博客下课创建多个文章分类。
'''
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=64)
blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
class Tag(models.Model):
'''
Tag - 文章关键字表
-nid
-title:文章关键字
-blog:对应博客表,一对多关系,一个博客下可床架你多个文章关键字
'''
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=64)
blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
class Article(models.Model):
'''
Article - 文章表
- nid
- title
- desc :摘要
- content :文章内容
- create_time :创建时间
- blog :对应博客表,一对多,一个博客下存在多篇文章
- category :对应文章分类表,一对多,一个文章分类存在多篇文章
- tag:对应文章关键字表,多对多,一篇文章存在多个关键字,一个关键字对应多个文章
'''
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=64)
desc = models.CharField(max_length=255)
# TextField - 大文本字段,可存储大量数据内容
content = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
category = models.ForeignKey(to='Category', to_field='nid', null=True)
tag = models.ManyToManyField(to='Tag', through='ArticleToTag', through_fields=('article', 'tag'))
class ArticleToTag(models.Model):
nid = models.AutoField(primary_key=True)
article = models.ForeignKey(to='Article', to_field='nid')
tag = models.ForeignKey(to='Tag', to_field='nid')
class Commit(models.Model):
'''
Commit - 评论表
-nid
-content :评论内容
-commit_time:发表评论时间
-user:对应用户表,一对多关系,一个用户可存在多条评论
-article:对应文章表,一对多关系,一篇文章下可存在多条评论
-parent_id:对应自身评论表,实现子评论,一对多关系,一条评论下可存在多条子评论
'''
nid = models.AutoField(primary_key=True)
# 后来加入,保证default为None才能插入字段
content = models.CharField(max_length=255, default=None)
commit_time = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(to='UserInfo', to_field='nid', null=True)
article = models.ForeignKey(to='Article', to_field='nid', null=True)
# parent_id=models.IntegerField() -- 不使用自关联,无法进行跨表查询,但也可以实现效果
parent_id = models.ForeignKey(to='self', to_field='nid', null=True)
class UpAndDown(models.Model):
'''
UpAndDown - 赞踩表
-nid
-is_up:布尔值,表示赞或者踩
-user :对应用户表,一对多关系,一个用户可以赞踩多次
-article:对应文章表,一对多关系,一片文章下可多次被赞踩
user和article进行联合唯一:单user 无法对 单article 多次点赞
'''
nid = models.AutoField(primary_key=True)
is_up = models.BooleanField()
user = models.ForeignKey(to='UserInfo', to_field='nid', null=True)
article = models.ForeignKey(to='Article', to_field='nid', null=True)
class Meta:
unique_together = (('user', 'article'),)
相关文章: