编写后端
This commit is contained in:
1
backend/app/models/__init__.py
Normal file
1
backend/app/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Models module
|
||||
BIN
backend/app/models/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/app/models/__pycache__/category.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/category.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/app/models/__pycache__/comment.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/comment.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/app/models/__pycache__/post.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/post.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/app/models/__pycache__/tag.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/tag.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/app/models/__pycache__/user.cpython-312.pyc
Normal file
BIN
backend/app/models/__pycache__/user.cpython-312.pyc
Normal file
Binary file not shown.
22
backend/app/models/category.py
Normal file
22
backend/app/models/category.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""
|
||||
分类模型
|
||||
"""
|
||||
import uuid
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class Category(models.Model):
|
||||
"""分类模型"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
name = fields.CharField(max_length=50, unique=True, description="分类名称")
|
||||
slug = fields.CharField(max_length=50, unique=True, description="URL别名")
|
||||
description = fields.TextField(null=True, description="分类描述")
|
||||
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||||
|
||||
class Meta:
|
||||
table = "categories"
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
49
backend/app/models/comment.py
Normal file
49
backend/app/models/comment.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
评论模型
|
||||
"""
|
||||
import uuid
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
"""评论模型"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
content = fields.TextField(description="评论内容")
|
||||
is_approved = fields.BooleanField(default=True, description="是否审核通过")
|
||||
|
||||
# 关联用户(评论者)
|
||||
author = fields.ForeignKeyField(
|
||||
"models.User",
|
||||
related_name="comments",
|
||||
on_delete=fields.CASCADE,
|
||||
description="评论者"
|
||||
)
|
||||
|
||||
# 关联文章
|
||||
post = fields.ForeignKeyField(
|
||||
"models.Post",
|
||||
related_name="comments",
|
||||
on_delete=fields.CASCADE,
|
||||
description="所属文章"
|
||||
)
|
||||
|
||||
# 自关联(回复)
|
||||
parent = fields.ForeignKeyField(
|
||||
"models.Comment",
|
||||
related_name="replies",
|
||||
on_delete=fields.CASCADE,
|
||||
null=True,
|
||||
description="父评论"
|
||||
)
|
||||
|
||||
# 时间戳
|
||||
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||||
updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
|
||||
|
||||
class Meta:
|
||||
table = "comments"
|
||||
ordering = ["created_at"]
|
||||
|
||||
def __str__(self):
|
||||
return f"Comment by {self.author.username} on {self.post.title}"
|
||||
94
backend/app/models/post.py
Normal file
94
backend/app/models/post.py
Normal file
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
文章模型
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
"""文章模型"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
title = fields.CharField(max_length=200, description="文章标题")
|
||||
slug = fields.CharField(max_length=200, unique=True, description="URL别名")
|
||||
content = fields.TextField(description="文章内容(Markdown)")
|
||||
summary = fields.TextField(null=True, description="文章摘要")
|
||||
cover_image = fields.CharField(max_length=500, null=True, description="封面图片URL")
|
||||
|
||||
# 关联用户(作者)
|
||||
author = fields.ForeignKeyField(
|
||||
"models.User",
|
||||
related_name="posts",
|
||||
on_delete=fields.CASCADE,
|
||||
description="作者"
|
||||
)
|
||||
|
||||
# 关联分类
|
||||
category = fields.ForeignKeyField(
|
||||
"models.Category",
|
||||
related_name="posts",
|
||||
on_delete=fields.SET_NULL,
|
||||
null=True,
|
||||
description="分类"
|
||||
)
|
||||
|
||||
# 标签(多对多)
|
||||
tags = fields.ManyToManyField(
|
||||
"models.Tag",
|
||||
related_name="posts",
|
||||
through="post_tags",
|
||||
description="标签"
|
||||
)
|
||||
|
||||
# 统计数据
|
||||
view_count = fields.IntField(default=0, description="浏览量")
|
||||
like_count = fields.IntField(default=0, description="点赞数")
|
||||
comment_count = fields.IntField(default=0, description="评论数")
|
||||
|
||||
# 状态:draft(草稿), published(已发布), archived(已归档)
|
||||
status = fields.CharField(
|
||||
max_length=20,
|
||||
default="draft",
|
||||
description="文章状态"
|
||||
)
|
||||
|
||||
# SEO
|
||||
meta_title = fields.CharField(max_length=200, null=True, description="SEO标题")
|
||||
meta_description = fields.TextField(null=True, description="SEO描述")
|
||||
|
||||
# 时间戳
|
||||
published_at = fields.DatetimeField(null=True, description="发布时间")
|
||||
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||||
updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
|
||||
|
||||
class Meta:
|
||||
table = "posts"
|
||||
ordering = ["-created_at"]
|
||||
indexes = [
|
||||
("status", "published_at"),
|
||||
("author", "status"),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
class PostTag(models.Model):
|
||||
"""文章-标签关联表"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
post = fields.ForeignKeyField(
|
||||
"models.Post",
|
||||
on_delete=fields.CASCADE,
|
||||
description="文章"
|
||||
)
|
||||
tag = fields.ForeignKeyField(
|
||||
"models.Tag",
|
||||
on_delete=fields.CASCADE,
|
||||
description="标签"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
table = "post_tags"
|
||||
unique_together = (("post", "tag"),)
|
||||
21
backend/app/models/tag.py
Normal file
21
backend/app/models/tag.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""
|
||||
标签模型
|
||||
"""
|
||||
import uuid
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
"""标签模型"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
name = fields.CharField(max_length=50, unique=True, description="标签名称")
|
||||
slug = fields.CharField(max_length=50, unique=True, description="URL别名")
|
||||
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||||
|
||||
class Meta:
|
||||
table = "tags"
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
28
backend/app/models/user.py
Normal file
28
backend/app/models/user.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
用户模型
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class User(models.Model):
|
||||
"""用户模型"""
|
||||
|
||||
id = fields.UUIDField(pk=True, default=uuid.uuid4)
|
||||
username = fields.CharField(max_length=50, unique=True, description="用户名")
|
||||
email = fields.CharField(max_length=255, unique=True, description="邮箱")
|
||||
password_hash = fields.CharField(max_length=255, description="密码哈希")
|
||||
avatar = fields.CharField(max_length=500, null=True, description="头像URL")
|
||||
bio = fields.TextField(null=True, description="个人简介")
|
||||
is_active = fields.BooleanField(default=True, description="是否激活")
|
||||
is_superuser = fields.BooleanField(default=False, description="是否超级用户")
|
||||
created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
|
||||
updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
|
||||
|
||||
class Meta:
|
||||
table = "users"
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def __str__(self):
|
||||
return self.username
|
||||
Reference in New Issue
Block a user