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

49 lines
1.6 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
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from blog.models import Article
#zr 评论数据模型
class Comment(models.Model):
#zr 评论正文最大长度300字符
body = models.TextField('正文', max_length=300)
#zr 评论创建时间,默认为当前时间
creation_time = models.DateTimeField(_('creation time'), default=now)
#zr 评论最后修改时间,默认为当前时间
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
#zr 评论作者,关联用户模型
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_('author'),
on_delete=models.CASCADE)
#zr 关联的文章
article = models.ForeignKey(
Article,
verbose_name=_('article'),
on_delete=models.CASCADE)
#zr 父级评论,支持评论回复功能
parent_comment = models.ForeignKey(
'self',
verbose_name=_('parent comment'),
blank=True,
null=True,
on_delete=models.CASCADE)
#zr 评论是否启用显示
is_enable = models.BooleanField(_('enable'),
default=False, blank=False, null=False)
class Meta:
#zr 按ID降序排列
ordering = ['-id']
#zr 设置单数和复数显示名称
verbose_name = _('comment')
verbose_name_plural = verbose_name
#zr 指定最新记录的依据字段
get_latest_by = 'id'
def __str__(self):
#zr 返回评论正文作为字符串表示
return self.body