0

我正在尝试编写一个模型管理器来获取所有已发布的条目,并且 start_date 是now. 奇怪的是,如果我将日期设置为过去的值(提前 1 分钟),一切正常,但如果我将日期设置在未来(提前 1 分钟),即使我等待更多时间,对象也不会显示测试时间超过 1 分钟。如果我只重新启动服务器什么都不做,则会显示该条目。但是每次发布条目时我都无法重新启动服务器。

有人看到这段代码有什么问题吗?或者这可以做不同的吗?

from django.utils import timezone

now = timezone.now()


def entries_published(queryset):
    """Return only the entries published"""
    return queryset.filter(
        models.Q(start_publication__lte=now) | \
        models.Q(start_publication=None),
        models.Q(end_publication__gt=now) | \
        models.Q(end_publication=None),
        status=PUBLISHED)


class EntryPublishedManager(models.Manager):
    """Manager to retrieve published entries"""

    def get_queryset(self):
        """Return published entries"""
        return entries_published(
            super(EntryPublishedManager, self).get_queryset())



class Entry(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    categories = models.ManyToManyField(Category, related_name='entries', blank=True, null=True)
    creation_date = models.DateTimeField(auto_now_add=True)
    start_publication = models.DateTimeField(blank=True, null=True, help_text="Date and time the entry should be visible")
    end_publication = models.DateTimeField(blank=True, null=True, help_text="Date and time the entry should be removed from the site")    
    status = models.IntegerField(choices=STATUS_CHOICES, default=DRAFT)

    objects = models.Manager()
    published = EntryPublishedManager()

我的看法是这样的:

class EntryListView(ListView):
    context_object_name = "entry_list"
    paginate_by = 20
    queryset = Entry.published.all().order_by('-start_publication') 

编辑:

如果我在视图now = timezone.now()的 def 内移动,则会获得正确的已发布条目。知道为什么不显示正确的吗?entries_publishedCategoryDetailEntryListView

这是我的CategoryDetail看法

#View for listing one category with all connected and published entries
class CategoryDetail(ListView):

    paginate_by = 20

    def get_queryset(self):
        self.category = get_object_or_404(Category, slug=self.kwargs['slug'])
        return Entry.published.filter(categories=self.category).order_by('-start_publication', 'title')

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CategoryDetail, self).get_context_data(**kwargs)
        # Add in the category
        context['category'] = self.category
        return context

编辑 2。

如果我将我的更改EntryListView为:

class EntryListView(ListView):

    model = Entry

    context_object_name = "news_list"
    paginate_by = 20

    def get_queryset(self):
        return Entry.published.all().order_by('-start_publication')

一切正常。奇怪,但我不在乎。

4

1 回答 1

0

已编辑:我认为您的问题是 now = timezone.now() 在您启动应用程序时设置。在您的 entries_published 方法中定义“现在”。

补充:我建议简化您的方法以确保调用类方法并完全跳过函数定义。

class EntryPublishedManager(models.Manager):
    """Manager to retrieve published entries"""
    def get_queryset(self):
        now = timezone.now()
        return Super(EntryPublishedManager, self).get_queryset().filter(
            models.Q(start_publication__lte=now) | \
            models.Q(start_publication=None),
            models.Q(end_publication__gt=now) | \
            models.Q(end_publication=None),
            status=PUBLISHED)
于 2014-03-12T17:17:52.377 回答