Trouble using django-mptt for nested comment system

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 03:59:42

问题


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.

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

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.


回答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.

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)


来源:https://stackoverflow.com/questions/18034530/trouble-using-django-mptt-for-nested-comment-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!