沒有過開發(fā)經(jīng)驗(yàn)的新手,拜托各位大神指點(diǎn)一下,我應(yīng)該怎么改進(jìn)下面我下面這種二級評論的設(shè)計(jì)?如果描述的不夠詳細(xì),我再補(bǔ)充。
ps: 我的思路,就是一篇文章和一級評論形成一對多的關(guān)系,然后一級評論和二級評論又形成一對多的關(guān)系。
models.py
class BlogComment(models.Model):
"""這是一級評論"""
user_name = models.CharField('Name', max_length=100) # 指定用戶名
body = models.TextField('Content') # 評論的主體
# 將一級評論關(guān)聯(lián)對應(yīng)的文章
article = models.ForeignKey('Article', verbose_name='Article',
on_delete=models.CASCADE)
class SubComment(BlogComment):
"""這是二級評論,繼承自一級評論,但是增加了一個(gè)parent_comment屬性"""
# 將二級評論關(guān)聯(lián)對應(yīng)的一級評論
parent_comment = models.ForeignKey('BlogComment', verbose_name='BlogComment',
on_delete=models.CASCADE)
froms.py中指定評論的表單
class BlogCommentForm(forms.ModelForm):
"""一級評論的表單"""
class Meta:
model = BlogComment # 指定一級評論的model
fields = ['user_name', 'body']
widgets = {
'user_name': forms.TextInput(attrs={
'required': 'required',
}),
'body': forms.Textarea(attrs={
'required': 'required',
}),
}
class SubCommentForm(BlogCommentForm):
"""二級評論的表單,繼承自一級評論的表單"""
class Meta:
model = SubComment # 制定二級評論的model
fields = copy.deepcopy(BlogCommentForm.Meta.fields)
widgets = copy.deepcopy(BlogCommentForm.Meta.widgets)
views.py
class CommentPostView(FormView):
"""一級評論視圖層"""
form_class = BlogCommentForm
template_name = 'blog/article.html'
def form_valid(self, form):
# 保存表單到數(shù)據(jù)庫
comment = form.save(commit=False)
comment.save()
return HttpResponseRedirect('/')
def form_invalid(self, form):
# ... 一些提示用戶表單輸入不合理的信息
class SubCommentView(CommentPostView):
"""二級評論視圖層,繼承與一級評論視圖層"""
# 覆蓋form_class成二級評論的表單
form_class = SubCommentForm
3 回答

夢里花落0921
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果評論只有2級,大體思路沒有問題。
但是你要想,你的二級評論,也是可以被評論的。所以有可能會(huì)有三級,四級。。評論。
這樣你的設(shè)計(jì)就會(huì)有問題。
這其實(shí)是一個(gè)比較經(jīng)典的問題了吧. 不過要是我去設(shè)計(jì),我會(huì)根據(jù)樹模型去設(shè)計(jì).
添加回答
舉報(bào)
0/150
提交
取消