如果在futu中设置了日期,则开始日期和结束日期的模型管理器不起作用

2024-05-21 01:58:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个模型管理器来获取所有发布的条目,并且开始日期是在过去的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') 

编辑:

如果我在entries_published的def中移动now = timezone.now()CategoryDetail视图将获得正确的发布条目。知道为什么EntryListView没有显示正确的吗?你知道吗

这是我的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')

一切正常。奇怪,但我不在乎。你知道吗


Tags: theselftruegetbymodelsdefcontext
1条回答
网友
1楼 · 发布于 2024-05-21 01:58:21

编辑时间: 我想你现在的问题是时区。现在()在启动应用程序时设置。在已发布的方法中定义“now”。你知道吗

添加: 我建议简化方法,确保调用类方法,并完全跳过函数定义。你知道吗

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)

相关问题 更多 >