跟踪Django中一个对象收到的视图数量 [英] Keeping track of how many views an object receives in Django

查看:106
本文介绍了跟踪Django中一个对象收到的视图数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了统计目的,我需要保存每次在Django中查看Model的某个实例的日志。我开始创建一个单独的模型Stats,其中包含一个 ManyToMany 字段到另一个存储访问日期和时间的模型。每次在视图中访问对象时,我会更新相关的 Stats 对象。

For statistical purposes, I need to keep a log of each time a certain instance of a Model is viewed in Django. I started off by creating a separate model, Stats, that contains a ManyToMany field to another Model that stores the date and time of the access. Every time the object is accessed in a view, I update the associated Stats object.

有两个问题这种方法(如果不是更多):

There are 2 problems with this approach (if not more):


  1. 它违反了在 GET 请求。

  2. 更重要的是,它的真的慢,特别是因为可以一次查看几个对象。这样会导致页面加载时出现可见的延迟。

  1. It violates the principle of not writing any data on a GET request.
  2. More importantly, it's really slow, especially because several objects can be viewed at once. This results in a visible delay when the page is loaded.

所以我的问题是,有没有更好的方法?如果没有,可以使用什么技术来加快速度,例如延迟写入数据库?我从来没有在Django中处理过这样的事情,所以任何建议都不会被赞赏。

So my question is, is there a better way of doing this? If not, what techniques are available to speed things up, such as delayed writes to the DB? I've never worked with that kind of thing in Django, so any advice would be appreciated.

推荐答案

似乎芹菜看起来是一个很好的选择。

Right, well, it seems Celery looks like a good option.

这篇关于跟踪Django中一个对象收到的视图数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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