-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Django 文章数据模型设计及演进 #3
Comments
回复打算支持盖楼,因此基于 MPTT 来设计的,也就是支持树形结构 class Reply(MPTTModel, BaseModel, SoftDeleteMixin, BaseReactStatMixin):
ref = TreeForeignKey('self', null=True, blank=True, related_name="replies", on_delete=models.SET_NULL,
verbose_name="引用")
creator = models.ForeignKey(WepostUser, verbose_name="回复人", on_delete=models.PROTECT)
content = models.TextField("内容", max_length=4096)
content_rendered = models.TextField("渲染内容", max_length=8192, default='')
#
creator_name = models.CharField("回复人名", max_length=150) # 冗余数据
reply_from = models.CharField("回复人来源", max_length=64, help_text="用户来源地点等信息")
reply_ip = models.GenericIPAddressField("回复IP") 点赞收藏分享等统计数据,提取出作为 class BaseReactStatMixin(models.Model):
# 基本反馈统计相关数据
like_count = models.PositiveIntegerField("喜欢数", default=0)
dislike_count = models.PositiveIntegerField("不喜欢数", default=0)
fav_count = models.PositiveIntegerField("收藏数", default=0)
share_count = models.PositiveIntegerField("分享数", default=0)
class Meta:
abstract = True |
给文章添加状态字段,习惯上是使用一个枚举来创建状态变量。 class PostState(IntEnum):
# 基本状态
DRAFT = 1
PUBLISHED = 6
@property
def label(self):
return _post_state_to_text[self]
_post_state_to_text = {
PostState.DRAFT: "草稿",
PostState.PUBLISHED: "已发布",
}
state = models.SmallIntegerField("状态", choices=PostState.choices(), default=PostState.DRAFT) |
上面的 Reply 模型忽略了 对 2)直接在 在 添加一个 post = models.ForeignKey(Post, on_delete=models.CASCADE, verbose_name="主题") |
Reply 模型添加 增加 order 字段,方便后续运营
|
目前的基本设计如下:
值得说明的是
BaseModel
继承了created
,updated
字段。SoftDeleteMixin
继承了_deleted_at
字段,此字段用于记录对应文章是否被软删除了。content
主要用来保存原始的编辑内容,而 content_rendered 缓存 渲染成的 html 格式文档。The text was updated successfully, but these errors were encountered: