Django queryset自定义管理器 - 刷新缓存 [英] Django queryset custom manager - refresh caching

查看:182
本文介绍了Django queryset自定义管理器 - 刷新缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的用户资料模型中,我有一个方法(转化为一个属性),返回一个基于另一个模型的自定义管理器的查询集。不易腐烂和易腐烂的物品,以及在物品模型级别,几个自定义管理员生活中包含逻辑(并返回查询),以确定物品是否灭亡。在用户配置文件中,一种方法可以返回类似于以下内容的方法:

  Item.live_objects.filter(seller = self.user) 

其中non_perished_objects是上述自定义管理员之一。



然而,如果添加了一个项目,则从未通过这些userprofile方法来反映。只有当重新启动服务器(和重新填充的查询缓存)时,结果是正确的。



有没有办法强制Django重新加载数据并删除缓存的数据?



提前感谢!



更新:

  class LiveItemsManager(models.Manager):

kwargs = {'perished':False,
'schedule__start_date__lte' datetime.datetime.now(),
'schedule__end_date__gt':datetime.datetime.now()}

def get_query_set(self):
return super(LiveItemsManager,self)。 get_query_set()。filter(** self.kwargs)

类项目(models.Model):
live_objects = LiveItemsManager()
perished = models.BooleanField(default = False )
seller = models.ForeignKey(User)

如你所见,还有一个计划模型,包含一个start_date,一个end_data和一个item_id字段。



I在UserProfile模型中,我有:

  def _get_live_items(self):
results = Item.live_objects.filter卖家= self.user)
返回结果

live_items =属性(_get_live_items)

问题是当调用live_items属性时,返回的结果只是缓存的结果。



(PS:不介意设置楷模;有一个原因为什么模型是他们是什么:))

解决方案

问题是,当经理是首先定义 - 这是在首次导入models.py时。因此,将计算用于 schedule__start_date schedule__end_date 的值,而不会更改。您可以通过在方法中移动 kwargs 声明来解决此问题:

  def get_query_set(self):
kwargs = {'perished':False,
'schedule__start_date__lte':datetime.datetime.now(),
'schedule__end_date__gt':datetime.datetime.now() }
return super(LiveItemsManager,self).get_query_set()。filter(** kwargs)

(将定义放入 __ init __()将不会有帮助,因为它将具有相同的效果:定义将在管理器实例化时进行评估,而不是定义,但是由于经理在定义模型时被实例化,所以这几乎是同一时间。)


In my userprofile model, I have a method (turned into a property) that returns a queryset based on another model's custom manager.

Concretely, a user can sell non-perishable and perishable items, and on the Item model level, several custom managers live that contain the logic (and return the querysets) for determining whether an item is perished or not. Within the userprofile, a method lives that returns something similar to:

Item.live_objects.filter(seller=self.user), 

where non_perished_objects is one of the said custom managers.

If however an item is added, it is never reflected through these userprofile methods. Only when restarting the server (and the queryset caches being refilled) are the results correct.

Is there a way to force Django to reload the data and drop the cached data?

Thanks in advance!

Update:

class LiveItemsManager(models.Manager):

    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}

    def get_query_set(self):
        return super(LiveItemsManager, self).get_query_set().filter(**self.kwargs)

class Item(models.Model):
    live_objects = LiveItemsManager()
    perished = models.BooleanField(default=False)
    seller = models.ForeignKey(User)

As you see, there's also a Schedule model, containing a start_date, an end_data and an item_id field.

In the UserProfile model, I have:

def _get_live_items(self):
    results = Item.live_objects.filter(seller=self.user)
    return results

live_items = property(_get_live_items)

The problem is that when calling the live_items property, the results returned are only the cached results.

(PS: Don't mind the setup of the models; there's a reason why the models are what they are :))

解决方案

The issue is that the kwargs are evaluated when the Manager is first defined - which is when models.py is first imported. So the values to be used against schedule__start_date and schedule__end_date are calculated then, and will not change. You can fix this by moving the kwargs declaration inside the method:

def get_query_set(self):
    kwargs = {'perished': False,
              'schedule__start_date__lte': datetime.datetime.now(),
              'schedule__end_date__gt': datetime.datetime.now()}
    return super(LiveItemsManager, self).get_query_set().filter(**kwargs)

(Putting the definition into __init__() won't help, as it will have the same effect: the definition will be evaluated at instantiation of the manager, rather than definition, but since the manager is instantiated when the model is defined, this is pretty much the same time.)

这篇关于Django queryset自定义管理器 - 刷新缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆