python - 请问我这样设计django文章的二级评论是否合理?
PHP中文网
PHP中文网 2017-04-18 09:56:18
0
3
591

没有过开发经验的新手,拜托各位大神指点一下,我应该怎么改进下面我下面这种二级评论的设计?如果描述的不够详细,我再补充。

ps: 我的思路,就是一篇文章和一级评论形成一对多的关系,然后一级评论和二级评论又形成一对多的关系。


models.py

class BlogComment(models.Model):
    """这是一级评论"""

    user_name = models.CharField('Name', max_length=100)  # 指定用户名
    body = models.TextField('Content')  # 评论的主体
    # 将一级评论关联对应的文章
    article = models.ForeignKey('Article', verbose_name='Article',
                                on_delete=models.CASCADE)

class SubComment(BlogComment):
    """这是二级评论,继承自一级评论,但是增加了一个parent_comment属性"""

    # 将二级评论关联对应的一级评论
    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):
        # 保存表单到数据库
        comment = form.save(commit=False)
        comment.save()
        
        return HttpResponseRedirect('/')

    def form_invalid(self, form):
        # ... 一些提示用户表单输入不合理的信息

class SubCommentView(CommentPostView):
    """二级评论视图层,继承与一级评论视图层"""
    # 覆盖form_class成二级评论的表单
    form_class = SubCommentForm 
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
洪涛

It can be designed like this, no problem. As for deeper comments, you only need to add the fields of the commentator and the commentee in the secondary comment table for identification. This is generally how the comments on my blog are done. For specific effects, please refer to: https://www.rapospectre.com/b... . Specific implementation: https://github.com/bluedazzle...

阿神

Thanks for the invitation

According to your idea, there is no problem. A first-level comment is a comment on a blog, and a second-level comment is actually a reply to a certain blog comment. It does not matter whether the person replying is the blogger or someone else, it is considered a reply.

I think it’s enough to consider the second level. Even the comments and replies in QQ space are only two levels. As for repeated replies, just show who replied to whom and then sort them by time. For details, please refer to the blog on the 2nd floor.

Personally, I think you can ignore Level 3 and Level 4 comments, as long as you understand that the comments are for the blog, and the replies are for the comments. Too many nested levels will cause lag.

Updated on 2016-11-17 19:17

class Comment(models.Model):
    """评论"""

    user = models.ForeignKey(User)          # 评论用户
    body = models.TextField()               # 评论内容
    article = models.ForeignKey(Article)    # 博客
    created_at = ...                        # 评论时间

class Reply(models.Model):
    """回复"""
    
    comment = models.ForeignKey(Comment)    # 评论
    from_user = models.ForeignKey(User)     # 回复用户
    to_user = models.ForeignKey(User)       # 被回复用户
    body = models.TextField()               # 回复内容
    created_at = ...                        # 回复时间

I will probably design the database like this. There will be multiple comments under a blog, and there will be multiple replies under each comment.

巴扎黑

If the comment is only level 2, there is no problem with the general idea.
But you have to think that your secondary comments can also be commented on. So there may be levels three and four. . Comment.
This will cause problems with your design.
This is actually a relatively classic question. But if I were to design it, I would design it based on the tree model.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!