麻烦使用django-mptt嵌套注释系统

时间:2022-03-13 14:35:17

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.

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

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.

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

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 %}

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:

最后,我的整个admin.py文件,因为我觉得这个问题的一部分可能是由于我通过管理员改变了一些事情:

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.

非常感谢您对此的任何帮助。

1 个解决方案

#1


1  

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.

得到了很棒的软件包作者Craig de Stigter的帮助。似乎问题是由于我在对特定注释的order_insertion_by字段(“点”)进行更改后未在模型树上使用rebuild()引起的。

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

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

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

#1


1  

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.

得到了很棒的软件包作者Craig de Stigter的帮助。似乎问题是由于我在对特定注释的order_insertion_by字段(“点”)进行更改后未在模型树上使用rebuild()引起的。

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

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

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