zoukankan      html  css  js  c++  java
  • 博客系统表设计

    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 = "评论点赞表"

  • 相关阅读:
    jsp 特殊标签
    poj 1753 Flip Game 高斯消元 异或方程组 求最值
    zoj 3155 Street Lamp 高斯消元 异或方程组 求方案数
    poj1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组 模板
    zoj 3930 Dice Notation 模拟
    zoj 3157 Weapon 线段树求逆序对数
    hdu 1242 Rescue BFS+优先队列
    hdu 3466 Proud Merchants 贪心+01背包
    zoj 3689 Digging 贪心+01背包
    hdu 2602 Bone Collector 01背包模板
  • 原文地址:https://www.cnblogs.com/morgana/p/8496456.html
Copyright © 2011-2022 走看看