2

Now I have the following code in the views.py:

class IndexView(generic.ListView):
    model = Question
    template_name = "qa_forum/index.html"
    context_object_name = 'question_list'
    paginate_by = 25

Its work good, but it retrieves all questions in the database (including closed and "future" questions).

In the models.py I have the following manager:

class ActiveManager(models.Manager):
    def get_query_set(self):
        return super(ActiveManager, self).get_query_set(). \
                .filter(pub_date__lte=timezone.now(), is_active=True
                ).order_by('-pub_date')

That helps to get only active question from the db.

But I don't know how to use it properly with the generic ListView.

Any advice would be greatly appreciated.

9Algorithm
  • 1,258
  • 2
  • 16
  • 22

1 Answers1

3

Instead of implementing modelManager you can set the queryset on the ListView class as:

class IndexView(generic.ListView):
    model = Question
    template_name = "qa_forum/index.html"
    context_object_name = 'question_list'
    paginate_by = 25
    queryset = Question.objects.filter(pub_date__lte=timezone.now(), 
                                       is_active=True).order_by('-pub_date')

If you want to go by modelManager method, you can set queryset as

    class IndexView(generic.ListView):
        #if you have set manger as active_objects in Question model
        queryset = Question.active_objects.filter()
Rohan
  • 52,392
  • 12
  • 90
  • 87