djangodjango-mptt

django-mptt count items in add_related_count?


How to add filter(is_public=True) to Question?

Category.objects.add_related_count(
  Category.objects.filter(is_public=True), 
  Question,
  'category',
  'question_counts',
  cumulative=True)

Solution

  • You can use the extra_filters=… parameter of the .add_related_count(…) method [Django-doc]:

    Category.objects.add_related_count(
        Category.objects.filter(is_public=True), 
        Question,
        'category',
        'question_counts',
        cumulative=True,
        extra_filters={'is_public': True}
    )