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

52 lines
1.8 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.

from django.conf import settings # 导入Django设置
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):
# 评论正文最大长度300字符
body = models.TextField('正文', max_length=300)
# 评论创建时间,默认当前时间
creation_time = models.DateTimeField(_('creation time'), default=now)
# 最后修改时间,默认当前时间
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
# 外键:评论作者,关联用户模型,级联删除
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_('author'),
on_delete=models.CASCADE)
# 外键:评论所属文章,关联文章模型,级联删除
article = models.ForeignKey(
Article,
verbose_name=_('article'),
on_delete=models.CASCADE)
# 自关联外键:父评论(用于实现嵌套评论),可为空
parent_comment = models.ForeignKey(
'self',
verbose_name=_('parent comment'),
blank=True,
null=True,
on_delete=models.CASCADE)
#是否启用/显示评论默认False需审核
is_enable = models.BooleanField(_('enable'),
default=False, blank=False, null=False)
class Meta:
ordering = ['-id'] # 按ID降序排列
verbose_name = _('comment') # 单数名称
verbose_name_plural = verbose_name # 复数名称
get_latest_by = 'id' # 获取最新评论的字段
# 返回评论正文作为字符串表示
def __str__(self):
return self.body