Good afternoon, below is the code from views.py which does a selection of all the articles in the section. But if the articles are tied to a child section, then nothing is selected. How can I select all articles on the root section to select all of all child sections ???

def article_section(request, category_slug=None): category = None object_list = Article.objects.all() if category_slug: category = get_object_or_404(Category, slug=category_slug) object_list = object_list.filter(category=category) paginator = Paginator(object_list, 3) page = request.GET.get('page') sections = Category.objects.all() try: articles = paginator.page(page) except PageNotAnInteger: articles = paginator.page(1) except EmptyPage: articles = paginator.page(paginator.num_pages) return render( request, 'articles/list.html', { 'page': page, 'category': category, 'sections': sections, 'articles': articles, } ) 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

First, you need to get all categories of children in relation to the selected one. If mptt is used, the issue is solved simply: categories = category.get_descendants(include_self=True) . If not used, the recursive function will help. After receiving the list of categories:

 object_list = object_list.filter(category__in=categories) 
  • I did not immediately understand, a little googling, found several similar examples. I did this: object_list = object_list.filter(category__in=category.get_descendants(include_self=True)) Works! Thank you very much for the help!!! - wedoca
  • For thanks, there is a "Accept" button. - Sergey Gornostaev