N
N
newvasyuki2020-11-19 14:55:01
Django
newvasyuki, 2020-11-19 14:55:01

How to display number of filtered descendants in mptt?

There are following models using django-mptt:

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

# жанры
class Genre(MPTTModel):
    name = models.CharField(max_length=255, unique=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['name']
        
# статьи        
class Article(models.Model):
    ...
    genre = TreeForeignKey(Genre, on_delete=models.CASCADE)
    published = models.BooleanField(default=False)


How to display a list of genres in a template along with the number of child articles, but filtered by published=True? The task is typical, it is not clear from the documentation.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question