
from haystack import indexes

from .models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().objects.select_related().filter(
            published=True
        ).exclude(
            current_version=None
        )


from django.utils import timezone
from haystack import indexes

from .models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().objects.published().select_related().filter(
            date__lte=timezone.now())

Fix the queryset for news indexing
Fix the queryset for news indexing

  
+ from django.utils import timezone
  from haystack import indexes
  
  from .models import Post
  
  
  class PostIndex(indexes.SearchIndex, indexes.Indexable):
      text = indexes.CharField(document=True, use_template=True)
  
      def get_model(self):
          return Post
  
      def index_queryset(self, using=None):
-         return self.get_model().objects.select_related().filter(
+         return self.get_model().objects.published().select_related().filter(
+             date__lte=timezone.now())
-             published=True
-         ).exclude(
-             current_version=None
-         )
  
Fix the queryset for news indexing
## Code Before:

from haystack import indexes

from .models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().objects.select_related().filter(
            published=True
        ).exclude(
            current_version=None
        )

## Instruction:
Fix the queryset for news indexing
## Code After:

from django.utils import timezone
from haystack import indexes

from .models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().objects.published().select_related().filter(
            date__lte=timezone.now())
