django 项目试炼blog

Posted zhuzhiwei-2019

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 项目试炼blog相关的知识,希望对你有一定的参考价值。

  1、确定表结构

技术图片

 

from django.contrib.auth.models import AbstractUser

class UserInfo(AbstractUser):
    """
    用户信息表, 继承session生成的auth_user表
    需要在setting中加入 AUTH_USER_MODEL = ‘app01.UserInfo‘
    """
    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_name = models.CharField(verbose_name=站点名称, max_length=64)
    theme = models.CharField(verbose_name=博客主题, max_length=32)

    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 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 Article(models.Model):
    """博客文章"""
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name=文章标题, max_length=50)
    desc = models.CharField(verbose_name=文章秒速, max_length=255)
    create_time = models.DateTimeField(verbose_name=创建时间, auto_now_add=True)
    content = models.TextField()

    comment_count = models.IntegerField(default=0)  #评论数默认为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)
    tag = models.ManyToManyField(
        to=Tag,
        through=Article2Tag,
        through_fields=(article, tag))  #创建中间模型,自己生成多对多的对应关系表

    def __str__(self):
        return self.title


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):
        v = self.article.title + --- + self.tag.title
        return v


class ArticleUpDown(models.Model):
    """点赞表,用户1对文章2进行了T操作"""

    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to=UserInfo, to_field=nid, null=True, on_delete=models.CASCADE)
    article = models.ForeignKey(to=Article, to_field=nid, null=True, on_delete=models.CASCADE)
    is_up = models.BooleanField(default=True)

    class Meta:   # 联合唯一 ,字段不能重复
        unique_together = [
            (article, user),
        ]


class Comment(models.Model):
    """评论表"""
    nid = models.AutoField(primary_key=True)
    content = models.CharField(verbose_name=评论内容, max_length=255)
    create_time = models.DateTimeField(verbose_name=创建时间, auto_now_add=True)  # 当前时间

    article = models.ForeignKey(verbose_name=评论文章, to=Article,to_field=nid, on_delete=models.CASCADE)
    user = models.ForeignKey(to=UserInfo, to_field=nid, null=True, on_delete=models.CASCADE)

    parent_comment = models.ForeignKey(self,null=True,on_delete=models.CASCADE)  #建立评论树

    def __str__(self):
        return self.content

 

 

 


2、重点内容:

  想在session自建表auth-user表中增加字段,需要继承AbstractUser来实现,并且需要在项目配置文件中加入  AUTH_USER_MODEL = ‘应用名.表名‘

    评论表表中的树结构

  技术图片

 

以上是关于django 项目试炼blog的主要内容,如果未能解决你的问题,请参考以下文章

Django 项目试炼blog -- 评论树的显示

Django 项目试炼blog -- 文章详情页1 -- 点赞功能

Django 项目试炼blog -- 文本编辑器的使用(文件存储,预览)

Django 项目试炼blog -- 文章详情页2 -- 前端样式的继承与楼评论显示

jdango 项目试炼blog--基于Form组件ajax用户注册

mini2440裸机试炼之—RTC闹钟中断,节拍中断