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

48 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
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from blog.models import Article
# Create your models here.
class Comment(models.Model):
#jrx: 评论正文最大长度300
body = models.TextField('正文', max_length=300)
#jrx: 评论创建时间,默认当前时间
creation_time = models.DateTimeField(_('creation time'), default=now)
#jrx: 评论最后修改时间,默认当前时间
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
#jrx: 评论作者(外键关联用户表)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_('author'),
on_delete=models.CASCADE) #jrx: 级联删除,用户删除则评论也删除
#jrx: 关联的文章(外键关联文章表)
article = models.ForeignKey(
Article,
verbose_name=_('article'),
on_delete=models.CASCADE) #jrx: 级联删除,文章删除则评论也删除
#jrx: 父评论(自关联,用于实现评论回复功能)
parent_comment = models.ForeignKey(
'self',
verbose_name=_('parent comment'),
blank=True,
null=True,
on_delete=models.CASCADE) #jrx: 级联删除,父评论删除则子评论也删除
#jrx: 评论是否启用(是否显示)
is_enable = models.BooleanField(_('enable'),
default=False, blank=False, null=False)
class Meta:
#jrx: 按id倒序排序最新评论在前
ordering = ['-id']
verbose_name = _('comment')
verbose_name_plural = verbose_name
get_latest_by = 'id'
#jrx: 打印模型实例时显示评论正文
def __str__(self):
return self.body