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.
ReviewAndAnalyzeOpenSourceS.../comments/models.py

46 lines
2.0 KiB

This file contains ambiguous Unicode characters!

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.

# zy: 评论数据模型模块 - 定义评论的数据结构和数据库表结构
from django.conf import settings
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
# zy: 导入博客文章模型,用于建立评论与文章的关联
from blog.models import Article
# zy: 评论数据模型类 - 继承自Django的Model基类对应数据库中的评论表
class Comment(models.Model):
body = models.TextField('正文', max_length=300)
creation_time = models.DateTimeField(_('creation time'), default=now)
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
# zy: 作者外键字段 - 关联到用户模型,删除用户时级联删除其评论
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_('author'),
on_delete=models.CASCADE)
# zy: 文章外键字段 - 关联到博客文章模型,删除文章时级联删除相关评论
article = models.ForeignKey(
Article,
verbose_name=_('article'),
on_delete=models.CASCADE)
# zy: 父评论自关联字段 - 实现评论回复功能,允许评论有父评论
parent_comment = models.ForeignKey(
'self',
verbose_name=_('parent comment'),
blank=True,
null=True,
on_delete=models.CASCADE)
# zy: 启用状态字段 - 控制评论是否显示,用于评论审核机制
is_enable = models.BooleanField(_('enable'),
default=False, blank=False, null=False)
# zy: 模型元数据配置类 - 定义模型的数据库和行为配置
class Meta:
ordering = ['-id'] # zy: 默认排序规则 - 按ID降序排列新的评论显示在前面
verbose_name = _('comment') # zy: 模型在Admin中的单数显示名称
verbose_name_plural = verbose_name # zy: 模型在Admin中的复数显示名称
get_latest_by = 'id'
def __str__(self):
return self.body