python3进阶开发-第一个仿博客园的项目(1)详解编程语言

首先我们要设计一下表结构:

UserInfo(用户信息表) ——–》一对一    ———–》Blog(博客信息表)

UserInfo(用户信息表) ———》一对多    ———–》Article UpDown(点赞表)

UserInfo(用户信息表) ———》一对多    ———–》Article Comment(评论表)

UserInfo(用户信息表)———-》一对多    ———–》Article(文章)

Blog(博客信息表)    —————-》 一对多   ———–》Category(个人博客文章分类表) 

Blog(博客信息表)    —————-》一对多   ————》Tag(标签表)

Category(个人博客文章分类表)–》 一对多 ————-》Article(文章表)

Tag(标签表)  ————————–》多对多  ————-》Article(文章表)

Article(文章表) ————————》一对一  ————-》ArticleDetail(文章详情表)

Article(文章表) ————————》一对多  ————–》Article UpDown(点赞表)

Article(文章表) ————————》一对多 —————》Article Comment(评论表)

 

from django.db import models 
# Create your models here. 
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") 
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)  # 个人博客标题 
theme = models.CharField(max_length=32)  # 博客主题 
def __str__(self): 
return self.title 
class Meta: 
verbose_name = "博客" 
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 "{}-{}".format(self.blog.title, 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)  # 文章标题 
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", 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, self.tag) 
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) 
def __str__(self): 
return self.content 
class Meta: 
verbose_name = "评论" 
verbose_name_plural = verbose_name

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/12199.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论