6.2 - BBS + BLOG系统

Posted alice-bj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.2 - BBS + BLOG系统相关的知识,希望对你有一定的参考价值。

一、简介

 

二、数据库

知识点:

1.继承AbstractUser
目的:为了使用用户认证组件 auth User
配置:AUTH_USER_MODEL = "blog.UserInfo"

class UserInfo(AbstractUser):
...


2.中介模型
多对多,第三张表自己生成。
class Article(models.Model):
...
tags = models.ManyToManyField(to=‘Tag‘, through=‘Article2Tag‘, through_fields=(‘article‘, ‘tag‘))

class Article2Tag(models.Model):
...
class Meta: # 联合唯一
unique_together = [
(‘article‘, ‘tag‘),
]

3.联合唯一
class ArticleUpDown(models.Model):
nid = models.AutoField(primary_key=True)
user = models.ForeignKey("UserInfo", null=True, on_delete=models.CASCADE)
article = models.ForeignKey(‘Article‘, null=True, on_delete=models.CASCADE)

class Meta: # 联合唯一
unique_together = [
(‘user‘, ‘article‘),
]

 

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片
from django.db import models
from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    """
    用户信息
    """
    nid = models.AutoField(primary_key=True)
    telephone = models.CharField(max_length=11, null=True, unique=True)
    avatar = models.FileField(upload_to=avatars/, default=avatars/default.png)
    create_time = models.DateTimeField(verbose_name=创建时间, auto_now_add=True)

    blog = models.OneToOneField(to=Blog, to_field=nid, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.username


class Blog(models.Model):
    """
    博客信息
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name=个人博客标题, max_length=64)
    site = models.CharField(verbose_name=个人博客后缀, max_length=32, unique=True)
    theme = models.CharField(verbose_name=博客主题, max_length=32)

    def __str__(self):
        return self.title


class Category(models.Model):
    """
    博主个人文章分类表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name=分类标题, max_length=32)

    blog = models.ForeignKey(verbose_name=所属博客, to=Blog, to_field=nid, on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Tag(models.Model):
    """
    博主个人文章标签表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name=标签名称, max_length=32)

    blog = models.ForeignKey(verbose_name=所属博客, to=Blog, to_field=nid, on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Article(models.Model):
    """
    文章信息
    comment_count up_count down_count 为了查询时,效率高。
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50, verbose_name=文章标题)
    desc = models.CharField(max_length=255, verbose_name=文章描述)
    create_time = models.DateTimeField(verbose_name=创建时间, auto_now_add=True)

    comment_count = models.IntegerField(default=0)
    up_count = models.IntegerField(default=0)
    down_count = models.IntegerField(default=0)

    user = models.ForeignKey(verbose_name=作者, to=UserInfo, to_field=nid, on_delete=models.CASCADE)
    category = models.ForeignKey(to=Category, to_field=nid, null=True, on_delete=models.CASCADE)
    tags = models.ManyToManyField(to=Tag, through=Article2Tag, through_fields=(article, tag))

    def __str__(self):
        return self.title


class ArticleDetail(models.Model):
    """
    文章详细表
    """
    nid = models.AutoField(primary_key=True)
    content = models.TextField()
    article = models.OneToOneField(to=Article, to_field=nid, on_delete=models.CASCADE)


class Article2Tag(models.Model):
    """
    多对多, 文章、标签的第三张表
    """
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(verbose_name=文章, to=Article, to_field=nid, on_delete=models.CASCADE)
    tag = models.ForeignKey(verbose_name=标签, to=Tag, to_field=nid, on_delete=models.CASCADE)

    class Meta:
        unique_together = [
            (article, tag),
        ]

    def __str__(self):
        return self.article.title + -- + self.tag.title


class ArticleUpDown(models.Model):
    """
    点赞,踩灭表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey("UserInfo", null=True, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, on_delete=models.CASCADE)

    class Meta:
        unique_together = [
            (user, article),
        ]


class Comment(models.Model):
    """
    评论表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(verbose_name=评论者, to=UserInfo, to_field=nid, on_delete=models.CASCADE)
    article = models.ForeignKey(verbose_name=评论文章, to=Article, to_field=nid, on_delete=models.CASCADE)
    content = models.CharField(verbose_name=评论内容, max_length=255)
    create_time = models.DateTimeField(verbose_name=创建时间, auto_now_add=True)

    parent_comment = models.ForeignKey(self, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.content
models.py

三、其它

 

以上是关于6.2 - BBS + BLOG系统的主要内容,如果未能解决你的问题,请参考以下文章

BBS+Blog项目开发

BBS+Blog项目流程及补充知识点

PLC 控制系统资源

6.2Android硬件访问服务编写系统代码

6.2Android硬件访问服务编写系统代码

typescript Angular 2测试片段。代码库https://developers.livechatinc.com/blog/category/programming/angular-2/