English 中文(简体)
Django似乎正在收尾约会时间。 现在( )
原标题:Django seems to be caching datetime.now()

我有一个模型 看起来像这个:

class Item(models.Model):
    ...
    publish_date = models.DateTimeField(default=datetime.datetime.now)
    ...

和一位经理长得像这个样子:

from datetime import datetime

class ItemManager(Manager):
    def published(self):
        return self.get_query_set().filter(publish_date__lte=datetime.now()

一种看起来像这样的景象:

class ItemArchive(ArchiveIndexView):
    queryset = Item.objects.published()
    date_field =  publish_date 

我的想法是,我可以调用 Troject.objects. 发表 并获得所有已发表的 Trojects 查询集。

问题是, Django 似乎在服务器启动时在管理器中执行 < code> datetime. now () 调用, 然后缓存该值。 所以如果今天是5月24日, 那么如果今天是5月23日, 我创建了一个 < code> Troit , 发布日期是5月23日, 并于5月22日启动服务器, 5月23日项目将不会出现在 < code> TrountArgive 视图中。 只要我重新启动 Apache, 5月23日项目就会正确出现在视图中 。

我怎么能强迫Django 执行 < code> datetime. 现在() 每次调用经理时都执行?

最佳回答

我相信这是由您将 queryset = unit.objects. 发布 () 作为类变量造成的。 当您最初导入您的 < code> 项目Allive 类时, 此行将执行一次。 您应该将该行移动到每次调用视图时都会执行的方法 。

问题回答

不要使用 queryset classet classet 变量, 并代之以覆盖 get_queryset 。 一般来说, queryset 是一个红色环。 如果您只指定 model model , Django 自动设置查询设置为 self. model. objects.all () 。 如果您需要过滤, 在100次中的99次中您需要覆盖 get_ queryset 才能安全。 请尝试以下方法 。

class ItemArchive(ArchiveIndexView):
    model = Item
    date_field =  publish_date 

    def get_queryset(self):
        return super(ItemArchive, self).get_queryset().published()

首先,请先使用 models.DateTime Field( 自动_ 现在_ add=True)

<强度>经编辑:

第二,使用 get_queryset 方法:

class ItemArchive(ArchiveIndexView):
    date_field =  publish_date 

    def get_queryset(self):
        return Item.objects.published()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...