Django项目之blog表设计(二)

设计表时需要注意的

1、表继承

复制代码
因为当你makemigrations和migrate的时候会自动生成auth_user表
所以创建用户表的时候可以用人家自定义的auth_user表, 如果你还想在表里增加一些字段,可以利用继承
from django.contrib.auth.models import AbstractUser
class UserInfo(AbstractUser): 
    pass
要是这样用继承的话需要在settings中配置一下:
AUTH_USER_MODEL ="应用名称.UserInfo"
复制代码

2、头像:可用FileField或者ImageField

avatar = models.FileField(verbose_name="头像",upload_to="avatar",default="/avatar/default.png") 
upload_to:上传到的具体位置
default:默认位置

3、创建时间

auto_now :无论是你添加还是修改对象,时间为你添加或者修改的时间
auto_now_add:是你当前创建的时间,当你更新对象时时间不会有变法
create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)

4、自己创建第三张表,并设置字段。。以下是具体操作

这样做的好处是:方便以后操作,可以在第三张关联表中增加或删除字段

复制代码
class Tag(models.Model):
    pass
class Article(models.Model):
    tags = models.ManyToManyField(to="Tag",through="article2tag",through_fields=('article', 'tag'))  #through_fields=('article', 'tag')相当于给这两个字段关联
class Article2tag(models.Model):
    article = models.ForeignKey(verbose_name="文章",to="Article")
    tag = models.ForeignKey(verbose_name="标签",to="Tag")
    class Meta:
        '''联合唯一'''
        unique_together = [
            ("article","tag")
        ]
复制代码

5、choices属性    

复制代码
type_choices = [
        (1,"编程语言"),
        (2,"软件设计"),
        (3,"前端系列"),
        (4,"数据库"),
        (5,"操作系统")
    ]
artcle_type_id = models.IntegerField(choices=type_choices,default=None)
复制代码

6、自关联的两种表示方式,假设为null的时候是文章赞

方式一
farther_comment = models.ForeignKey(to="Comment",verbose_name="父级评论",null=True,blank=True) 方式二
farther_comment = models.ForeignKey("self",verbose_name="父级评论",null=True,blank=True)

7、联合唯一的表示方式

class Meta:
    '''联合唯一'''
    unique_together = ("user_id","comment_id",)
    verbose_name_plural = "评论点赞表"
 

数据库表设计

    class UserInfo(AbstractUser):
    """
    用户信息表
    """
    # verbose_name 设置在admin中的别名, unique唯一, null是否可为空
    nid = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=11, null=True, unique=True, verbose_name="手机号")
    # upload_to前端传送过来的文件都保存在这个文件夹,如果不存在,自动创建
    avatar = models.FileField(upload_to="avatars/", default="avatars/default.png", verbose_name="照片")
    # auto_now_add自动添加当前时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    blog = models.OneToOneField(to="Blog", to_field="nid", null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.username

    class Meta:
        # 设置的 admin 后台管理中的别名
        verbose_name = "用户"
        verbose_name_plural = verbose_name
    
class Category(models.Model):
    """
    文章分类表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=24)
    # 外键关联博客,一个博客站点可以有多个分类
    blog = models.ForeignKey(to="Blog", to_field="nid", on_delete=models.DO_NOTHING)
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "文章分类"
        verbose_name_plural = verbose_name
    
class Tags(models.Model):
    """
    标签
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32, verbose_name="标签名")
    blog = models.ForeignKey(to="Blog", to_field="nid", on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name
    
class Article(models.Model):
    """
    文章表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=128, verbose_name="文章标题")
    desc = models.CharField(max_length=255, verbose_name="文章描述")
    user = models.ForeignKey(to="UserInfo", to_field="nid", on_delete=models.DO_NOTHING)
    category = models.ForeignKey(to="Category", to_field="nid", null=True,on_delete=models.CASCADE)
    tags = models.ManyToManyField(to="Tags", through="Article2Tag", through_fields=("article", "tag"))
    read_count = models.IntegerField(verbose_name="阅读数", default=0)
    comment_count = models.IntegerField(verbose_name="评论数", default=0)
    up_count = models.IntegerField(verbose_name="点赞数", default=0)
    down_count = models.IntegerField(verbose_name="踩数", default=0)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "文章"
        verbose_name_plural = verbose_name
    
class ArticleDetail(models.Model):
    """
    文章详情表
    """
    nid = models.AutoField(primary_key=True)
    content = models.TextField()
    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "文章详情表"
        verbose_name_plural = verbose_name
    
class Article2Tag(models.Model):
    """
    文章和标签的多对多关系表
    """
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.CASCADE)
    tag = models.ForeignKey(to="Tags", to_field="nid", on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return "{}-{}".format(self.article.title, self.tag.title)

    class Meta:
        verbose_name = "文章 -- 标签"
        verbose_name_plural = verbose_name
    
class ArticleUpDown(models.Model):
    """
    点赞/踩表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to="UserInfo", to_field="nid", on_delete=models.DO_NOTHING)
    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.DO_NOTHING)
    is_up = models.BooleanField(default=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        unique_together = (("article", "user"),)
        verbose_name = "文章点赞"
        verbose_name_plural = verbose_name
    
class Comment(models.Model):
    """
    评论表
    """
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.DO_NOTHING)
    user = models.ForeignKey(to="UserInfo", to_field="nid", on_delete=models.DO_NOTHING)
    content = models.CharField(max_length=255)
    parent_comment = models.ForeignKey("self", null=True, on_delete=models.DO_NOTHING, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = verbose_name

执行数据库建表操作:

python manage.py makemigrations
python manage.py migrate
 
posted @ 2020-04-06 12:09  DeyouKong  阅读(213)  评论(0编辑  收藏  举报