############################################
# 博客项目 # 拿到项目之后第一步就是需求分析 # 需求分析之后就是表设计 # 需要什么表: # 1,userinfo:用户名,密码,头像, # 2,文章表:title,publish_data,desc,author, # 3,评论表:user,时间,内容,关联的文章,是谁的子评论,文章和评论什么关系,一对多, # 3.1 文章详情表:info, # 4,标签:标签名,标签和文章,多对多, # 5,分类:分类名,分类和文章,多对多, # 6,点赞表:是赞,还是踩,点赞时间,关联文章,谁点的,关联user,
######################################
因为当你makemigrations和migrate的时候会自动生成auth_user表 所以创建用户表的时候可以用人家自定义的auth_user表, 如果你还想在表里增加一些字段,可以利用继承 from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser): pass 要是这样用继承的话需要在settings中配置一下: AUTH_USER_MODEL ="应用名称.UserInfo"
########################################
头像:可用FileField或者ImageField avatar = models.FileField(verbose_name="头像",upload_to="avatar",default="/avatar/default.png") upload_to:上传到的具体位置 default:默认位置
#######################################
创建时间 auto_now :无论是你添加还是修改对象,时间为你添加或者修改的时间 auto_now_add:是你当前创建的时间,当你更新对象时时间不会有变法 create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)
#########################################
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") ]
###########################################
choices属性 type_choices = [ (1,"编程语言"), (2,"软件设计"), (3,"前端系列"), (4,"数据库"), (5,"操作系统") ] artcle_type_id = models.IntegerField(choices=type_choices,default=None)
#######################################
自关联的两种表示方式,假设为null的时候是文章赞 方式一 farther_comment = models.ForeignKey(to="Comment",verbose_name="父级评论",null=True,blank=True) 方式二 farther_comment = models.ForeignKey("self",verbose_name="父级评论",null=True,blank=True)
#######################################
from django.db import models from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser): """ 用户信息表 """ nid = models.AutoField(primary_key=True) phone = models.CharField(max_length=11, null=True, unique=True) avatar = models.FileField(upload_to="avatars/", default="avatars/default.png", verbose_name="头像") create_time = models.DateTimeField(auto_now_add=True) blog = models.OneToOneField(to="Blog", to_field="nid", null=True) def __str__(self): return self.username class Meta: verbose_name = "用户" verbose_name_plural = verbose_name class Blog(models.Model): """ 博客信息 """ nid = models.AutoField(primary_key=True) title = models.CharField(max_length=64) # 个人博客标题 site = models.CharField(max_length=32, unique=True) # 个人博客后缀 theme = models.CharField(max_length=32) # 博客主题 def __str__(self): return self.title class Meta: verbose_name = "blog站点" verbose_name_plural = verbose_name class Category(models.Model): """ 个人博客文章分类 """ nid = models.AutoField(primary_key=True) title = models.CharField(max_length=32) # 分类标题 blog = models.ForeignKey(to="Blog", to_field="nid") # 外键关联博客,一个博客站点可以有多个分类 def __str__(self): return self.title class Meta: verbose_name = "文章分类" verbose_name_plural = verbose_name class Tag(models.Model): """ 标签 """ nid = models.AutoField(primary_key=True) title = models.CharField(max_length=32) # 标签名 blog = models.ForeignKey(to="Blog", to_field="nid") # 所属博客 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=50, verbose_name="文章标题") # 文章标题 desc = models.CharField(max_length=255) # 文章描述 create_time = models.DateTimeField(auto_now_add=True) # 创建时间 --> datetime() # 评论数 comment_count = models.IntegerField(verbose_name="评论数", default=0) # 点赞数 up_count = models.IntegerField(verbose_name="点赞数", default=0) # 踩 down_count = models.IntegerField(verbose_name="踩数", default=0) category = models.ForeignKey(to="Category", to_field="nid", null=True) user = models.ForeignKey(to="UserInfo", to_field="nid") tags = models.ManyToManyField( # 中介模型 to="Tag", through="Article2Tag", through_fields=("article", "tag"), # 注意顺序!!! ) 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.OneToOneField(to="Article", to_field="nid") 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") tag = models.ForeignKey(to="Tag", to_field="nid") def __str__(self): return "{}-{}".format(self.article.title, self.tag.title) class Meta: unique_together = (("article", "tag"),) verbose_name = "文章-标签" verbose_name_plural = verbose_name class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey(to="UserInfo", null=True) article = models.ForeignKey(to="Article", null=True) is_up = models.BooleanField(default=True) 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") user = models.ForeignKey(to="UserInfo", to_field="nid") content = models.CharField(max_length=255) # 评论内容 create_time = models.DateTimeField(auto_now_add=True) parent_comment = models.ForeignKey("self", null=True, blank=True) # blank=True 在django admin里面可以不填 def __str__(self): return self.content class Meta: verbose_name = "评论" verbose_name_plural = verbose_name
##################################################
############################################
来源:https://www.cnblogs.com/andy0816/p/12518867.html