You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
"""
评论系统数据模型定义
支持多级评论回复功能
"""
from django . conf import settings
from django . db import models
from django . utils . timezone import now
from django . utils . translation import gettext_lazy as _
from blog . models import Article
class Comment ( models . Model ) :
"""
评论模型
支持对文章的评论和评论之间的回复
"""
# 评论内容
body = models . TextField ( ' 正文 ' , max_length = 300 ) # 评论正文, 限制300字
# 时间戳
creation_time = models . DateTimeField ( _ ( ' 创建时间 ' ) , default = now )
last_modify_time = models . DateTimeField ( _ ( ' 最后修改时间 ' ) , default = now )
# 关联关系
author = models . ForeignKey (
settings . AUTH_USER_MODEL ,
verbose_name = _ ( ' 作者 ' ) ,
on_delete = models . CASCADE ) # 用户删除时删除其所有评论
article = models . ForeignKey (
Article ,
verbose_name = _ ( ' 文章 ' ) ,
on_delete = models . CASCADE ) # 文章删除时删除其所有评论
# 父级评论,实现评论回复功能
parent_comment = models . ForeignKey (
' self ' , # 自关联
verbose_name = _ ( ' 父级评论 ' ) ,
blank = True ,
null = True ,
on_delete = models . CASCADE ) # 父评论删除时删除子评论
# 评论状态
is_enable = models . BooleanField (
_ ( ' 是否启用 ' ) ,
default = False , # 默认需要审核
blank = False ,
null = False
)
class Meta :
""" 元数据配置 """
ordering = [ ' -id ' ] # 按ID降序, 新的评论在前
verbose_name = _ ( ' 评论 ' )
verbose_name_plural = verbose_name
get_latest_by = ' id '
def __str__ ( self ) :
"""
字符串表示
返回评论内容的前50个字符
"""
return self . body [ : 50 ] + ' ... ' if len ( self . body ) > 50 else self . body