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.
# comments/models.py
# DZQ: 评论模块的数据模型定义文件
from django . db import models
from django . conf import settings
# DZQ: 评论数据模型类 - 用于存储博客文章的评论信息
class Comment ( models . Model ) :
# DZQ: 外键关联到博客文章, CASCADE表示文章删除时评论也删除
post = models . ForeignKey (
' blog.Post ' ,
on_delete = models . CASCADE ,
verbose_name = " 所属文章 " ,
related_name = " comments " #DZQ: 通过related_name可从文章对象反向查询评论
)
# DZQ: 外键关联到用户模型, CASCADE表示用户删除时评论也删除
author = models . ForeignKey (
settings . AUTH_USER_MODEL ,
on_delete = models . CASCADE ,
verbose_name = " 评论者 " ,
related_name = " comments " # DZQ: 通过related_name可从用户对象反向查询评论
)
# DZQ: 评论内容字段, 使用TextField支持长文本
content = models . TextField ( verbose_name = " 评论内容 " )
# DZQ: 评论时间字段, auto_now_add=True表示创建时自动设置当前时间
created_time = models . DateTimeField ( auto_now_add = True , verbose_name = " 评论时间 " )
# DZQ: 模型元数据配置类
class Meta :
verbose_name = " 评论 " # DZQ: 单数形式的显示名称
verbose_name_plural = " 评论 " # DZQ: 复数形式的显示名称
ordering = [ ' -created_time ' ] #DZQ: 默认按评论时间倒序排列
#DZQ: 对象字符串表示方法, 用于在admin等界面显示
def __str__ ( self ) :
return f ' { self . author . username } 评论了《 { self . post . title } 》 '