Odd Django queryset behavior trying to filter use 'lte' filer on datetime field

I'm seeing some weird behavior with one of my models and am having a hard time tracing down what's going on. My application has a relatively simple 'posts' model that I'm trying to feed into a ListView:

# models.py
class Posts(models.Model):
    draft = 0
    published = 1
    status_choices = [(draft, "Draft"), (published, "Published")]

    id = models.AutoField(primary_key=True, unique=True)
    title = models.CharField(max_length=500)
    slug = models.SlugField(max_length=500, unique=True)
    text = models.TextField(null=False)
    html = models.TextField(blank=True, null=True)
    status = models.IntegerField(choices=status_choices, default=draft)
    create_date = models.DateTimeField(default=timezone.now, verbose_name="Post date")
    update_date = models.DateTimeField(default=timezone.now, verbose_name="Last edited")

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        savetime = datetime.now().replace(microsecond=0)
        if savetime != self.create_date     # set the update date if needed
            self.update_date = savetime
        if not self.slug:
            self.slug = slugify(self.title) # make sure the post gets a slug
        self.html = md.convert(self.text)   # convert markdown to HTML for storage
        return super().save(*args, **kwargs)

# views.py
class PostList(ListView):
    context_object_name = "posts"
    model = Posts
    queryset = (
        Posts.objects.all()
        .filter(create_date__lte=datetime.now(), status__exact=1)
        .order_by("-create_date")
    )
    template_name = "post.html"
    paginate_by = 20
    paginate_orphans = 2

But when I create a new post, it won't show in the template until I restart the app. I think it might have something to do with the create_date__lte=datetime.now() filter, because when I take that out, it works fine. (I don't want to take it out... I want to be able to future date a post and have it show up after that time.)

But even with the filter in place, when I look at the query in the Django shell, it seems to work--in the example below, POST1, POST2 and POST3 are all in the past, but POST4 is future dated.

>>> p = Posts.objects.all().filter(create_date__lte=datetime.now(), status__exact=1).order_by("-create_date")
>>> p
<QuerySet [<Posts: POST3>, <Posts: POST2>, <Posts: POST1>]>
>>> p = Posts.objects.all().filter(status__exact=1).order_by("-create_date")
>>> p
<QuerySet [<Posts: POST4>, <Posts: POST3>, <Posts: POST2>, <Posts: POST1>]>
>>>

What's going on here?

Back to Top