from django.db import models # Create your models here. from django.contrib.auth.models import AbstractUser #用户表#继承了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") blog=models.OneToOneField(to="Blog",to_field=‘nid‘,null=True) #定义__str__方法,直接输出对象名时,打印的是username的值 def __str__(self): return self.username class Meta: #verbose_name 给模型起一个更可读性的名字 verbose_name = "用户信息" #verbose_name_plural 模型的复数形式指定 verbose_name_plural = verbose_name #博客表#创建博客标题 /创建博客主题class Blog(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=64) #博客标题 theme=models.CharField(max_length=32) #博客主题 def __str__(self): return self.title class Mede: verbose_name="博客" verbose_name_plural=verbose_name #个人博客分类表#创建分类标题/外键关联博客,一个博客站点可以有多个分类#建立 与 博客一对多关系 一个博客里有多个分类class Category(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=64) blog=models.ForeignKey(to="Blog",to_field="nid") def __str__(self): return self.title class Mede: 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) desc = models.CharField(max_length=255) create_time = models.DateTimeField(auto_now_add=True) category = models.ForeignKey(to="Category", to_field="nid", null=True) user = models.ForeignKey(to="UserInfo") 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 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, self.tag) class Meta: unique_together = (("article", "tag"),) 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 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") content = models.CharField(max_length=255) # 评论内容 create_time = models.DateTimeField(auto_now_add=True) parent_comment = models.ForeignKey("self", null=True) def __str__(self): return self.content class Meta: verbose_name = "评论" verbose_name_plural = verbose_name
原文地址:https://www.cnblogs.com/yangli0504/p/9255452.html
时间: 2024-11-13 06:49:33