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 import forms
from django.forms import ModelForm
from .models import Comment
#ssj
# 定义一个用于处理评论数据的表单类,继承自 Django 的 ModelForm
class CommentForm(ModelForm):
# 自定义字段:parent_comment_id
# 用于存储当前评论所回复的父评论的 ID
parent_comment_id = forms.IntegerField(
widget=forms.HiddenInput, # 使用隐藏输入框(HTML <input type="hidden">),不在页面上显示
required=False # 非必填字段,因为一级评论没有父评论
)
class Meta:
model = Comment # 关联的数据库模型为 Comment
fields = ['body'] # 表单中需要包含的模型字段,仅包含 'body'(评论正文)
# 注意:其他字段如 author、article、creation_time 等通常在视图中自动填充,不暴露给用户