• Django 项目试炼blog(1)


      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 = '应用名.表名'

    AUTH_USER_MODEL = 'app01.UserInfo'
    

      

        评论表表中的树结构

      

  • 相关阅读:
    hash256 算法,sv实现
    systemverilog.vim
    修改本地git的用户名密码
    深度学习好文推荐
    fiddle免费汉化博客及网站提示异常
    appium自动化测试实现
    monkey使用及其常用命令
    JMeter接口测试,简单注意服务器名称别输错就行
    自动化真机测试代码易视云不清缓存登录
    Unity在 TextMeshPro 中使用中文【转自 chutianshu / AwesomeUnityTutorial】
  • 原文地址:https://www.cnblogs.com/zhuzhiwei-2019/p/10733547.html
Copyright © 2020-2023  润新知