0

I am using Django 3.2 and django-mppt 0.13.4

This is my (simplified) model:

/path/to/myapp/models.py

class Comment(MPTTModel, MyTimestampedModel):

    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,  related_name='children')

    content = models.TextField(default='')
    markdown_content = models.TextField(default='')

    attachment = models.ImageField()     


    class MPTTMeta:
        order_insertion_by = ['created_at']

    class Meta:
        permissions = [('can_moderate_comments', 'Can moderate comments'),
                       ('can_attach_file', 'Can attach file'),]


class Commentable(models.Model, Foo):
    accept_comments = models.BooleanField(default=True, blank=True)    
    comments = GenericRelation(Comment)
    
    # ... other fields


class Article(Commentable, FooBar):
    pass

/path/to/myapp/views.py

class ArticleDetailView(DetailView):
    model = Article    

    def get_context_data(self, **kwargs):
       context = super(ArticleDetailView, self).get_context_data(**kwargs)
             
       article = self.get_object()
       # ....

       context['comments'] = article.comments.all() 

From the mptt documentation

<ul class="root">
    {% recursetree nodes %}
        <li>
            {{ node.name }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

/path/to/myapp/templates/myapp/article_detail.html

       {% recursetree comments %}
           <li>
                 {{ node.markdown_content }}
                 {% if not node.is_leaf_node %}
                    {{node.get_descendant_count}} Replies
                    <ul class="children">
                    {{ children }} 
                    </ul>
                 {% endif %}
           </li>            
        {% endrecursetree %}  

I add three comments to the database (via the shell command), with the following heirarchical relation:

Comment 1 (id=1)
  Reply To Comment 1 (id=3)
    Reply To Reply to Comment 1 (id=4)
Comment 2 (id=2)

When I type the following at the command prompt:

Comment.objects.filter(id=1).get_children() 
=> 1

Comment.objects.filter(id=1).get_descendent_count()
=> 2

However (focusing on only the first comment for now), in my template, although the node.get_descendant_count variable matches that obtained directly from the DB, the children is a null set in the template - whereas when accessing the database directly, the correct number of (direct) children is returned.

Why is the template not returning the comment's children correctly - and how do I fix this?

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • "comment1:2 Replies, comment3:1 Replies, comment4, comment2". Is this the correct result? If it's correct, no issues in the mptt. Just got the right result on my local Django/mptt. – Raphael Oct 25 '21 at 19:27
  • @Devstorm21 No, that is not the correct result. The correct result is what I showed in the console print out in my question. Note, it is probably better to refer to `get_children().count` and `get_descendant_count()` as number of replies is too ambiguous. – Homunculus Reticulli Oct 26 '21 at 11:16
  • did you read my answer? – Raphael Oct 27 '21 at 12:10
  • @DanielKaminski Yes. Did you read my question? You seemed to have ignored the fact that I am following the documentation and it is still not working (I gave examples), you merely wrote an answer telling me that it works correctly. – Homunculus Reticulli Oct 30 '21 at 11:24
  • updated the answer. I am sure the result is correct. right? – Raphael Oct 30 '21 at 12:34

1 Answers1

0

No need to fix it because it works correctly. Please read the following notes carefully from https://django-mptt.readthedocs.io/en/latest/templates.html

Note the special variables node and children. These are magically inserted into your context while you’re inside the recursetree tag.

node is an instance of your MPTT model.

children : This variable holds the rendered HTML for the children of node.

And got a comment from mptt_tags.py.

Iterates over the nodes in the tree, and renders the contained block for each node. This tag will recursively render children into the template variable {{ children }}. Only one database query is required (children are cached for the whole tree)

I've got the following correct result.

<li>
    comment1
      2 Replies
  <ul class="children">
    <li>comment3
        1 Replies
        <ul class="children">
            <li>
            comment4
            </li>            
        </ul>           
    </li>            
  </ul>           
</li>
Raphael
  • 517
  • 4
  • 18