使用django-mptt进行嵌套注释系统时出现问题 [英] Trouble using django-mptt for nested comment system

查看:132
本文介绍了使用django-mptt进行嵌套注释系统时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用django-mptt建立一个简单的嵌套注释系统,但是我遇到了一些问题.如果有人可以看看并告诉我我做错了什么,我将非常感激.

I'm trying to set up a simple nested comment system using django-mptt, but I'm running into a few issues. If someone could take a look and tell me what I'm doing wrong, I would be really grateful.

到目前为止,我仅设置了特定帖子的评论显示;目前,所有创建/更新/删除操作都是通过管理员进行的.我遇到的问题之一是,有时当我尝试在管理员中创建/更新/删除时,出现属性错误'NoneType'对象没有属性'tree_id'".另一个是通过管理员更改注释实例上的"order_insertion_by"("points"字段)中指定的字段的整数值(当我导航到该页面时,有时会导致ValueError"cache_tree_children以错误的顺序传递节点")显示帖子和评论.

So far, I've only set up the display of comments for a particular post; any creating/updating/deleting is for the time being happening through the admin. One of the issues I'm having is that sometimes when I try to create/update/delete in the admin, I get the Attribute Error "'NoneType' object has no attribute 'tree_id'". Another is that changing the integer value of the field specified in "order_insertion_by" (a "points" field) on comment instances through the admin sometimes causes the ValueError "cache_tree_children was passed nodes in the wrong order" when I navigate to the page that should display the post and comments.

此外,有时某些注释会出现在错误的父项下,有时甚至根本不会出现.

Also, sometimes certain comments appear under the wrong parent, and occasionally do not appear at all.

以下是我的评论模型的相关部分:

Here are the relevant parts of my comment model:

class Comment(MPTTModel):
    posting = models.ForeignKey(Posting)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
    points = models.IntegerField(
        default=0,
    )

    class MPTTMeta:
        order_insertion_by = ['points']

以及我用来显示特定帖子评论的模板的相关部分:

And the relevant parts of the template I'm using to display the comments for a particular post:

{% load mptt_tags %}
{% with posting.comment_set.all as comments %}
<ul class="root">
    {% recursetree comments %}
        <li>
            {{ node.message }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>
{% endwith %}

最后,是整个admin.py文件,因为我觉得问题的一部分可能是由于我通过admin进行更改而引起的:

And, finally, the entirety of my admin.py file, because I feel like part of the issue may be caused by my changing things through the admin:

from django.contrib import admin
from django.forms import ModelForm, Textarea
from postings.models import Posting, Comment

class PostingForm(ModelForm):

    class Meta:

        model = Posting
        widgets = {
            'title': Textarea(attrs={'cols': 75, 'rows': 5}),
            'message': Textarea(attrs={'cols': 75, 'rows': 15}),
        }

class CommentForm(ModelForm):

    class Meta:

        model = Comment
        widgets = {
            'message': Textarea(attrs={'cols': 75, 'rows': 15}),
        }

class CommentInline(admin.TabularInline):
    model = Comment
    form = CommentForm

class PostingAdmin(admin.ModelAdmin):
    inlines = [CommentInline]
    list_display = ('title', 'posted', 'variety', 'points', 'user')
    form = PostingForm

admin.site.register(Posting, PostingAdmin)

非常感谢您对此提供的帮助.

Thanks so much for any help with this.

推荐答案

令人敬畏的软件包作者Craig de Stigter对此提供了一些帮助.似乎是由于我对特定注释的order_insertion_by字段(点")进行了更改之后,我没有在模型的树上使用rebuild()引起的.

Got some help from the awesome package author, Craig de Stigter, on this. Seems the issues were caused by my not using rebuild() on the model's tree after making changes to the order_insertion_by field ("points") of particular comments.

根据他的建议,我修改了我的Comment模型表单的save()方法以包括模型的重建:

Per his suggestion, I modified my Comment model form's save() method to include a rebuilding of the model:

def save(self, *args, **kwargs):
    Comment.objects.rebuild()
    return super(CommentForm, self).save(*args, **kwargs)

这篇关于使用django-mptt进行嵌套注释系统时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆