What about this? (disclaimer: I'm just starting in Django)
First, we create a model where you simply have the name of the view
and the number of times hitted. This model increases it's "counter"
variable each time it is saved.
class NbViews(models.Model)
viewname=models.TextField(maxlength=300)
counter=models.IntegerField(default=0)
def save(self):
self.counter+=1
super(NbViews, self).save()
Then, a middleware where you do something like this:
class ViewCounterMiddleware:
def process_view(self, request, view_func, view_args, view_kwargs):
if view in database:
view=search_view_func_in_database()
view.save()
else:
view=ViewCounter(viewname=view_func.__name__)
view.save()
return None
What you actually do is look if the name of the view called i.e.
"view_func.__called__" is in the database, and otherwise you simply
create it and in any case increase the counter.
I did something similar to keep track of the IP addresses visiting my
site. Of course, it probably has drawbacks, as it might charge the
database with so many commits, and probably there's a better way of
keeping track of the views that the view_func.__name__ thing :-)
Hope it helps,
G
On 7/31/06, Jay Klehr <[EMAIL PROTECTED]> wrote:
>
> I'm interested in what the community suggests on this topic, as it's
> something I'd like to implement as well. However, one word of advice if
> you're using MySQL and its Query Cache... try to keep the "views"
> column outside of your models table since updating that table on every
> hit would basically make the query cache for that table irrelevant. As
> I understand it, the Query Cache for a query is wiped if the table that
> is used in the cached query is changed after the query is cached. In
> this case the table would be changed on every load, so the query would
> never be cached. So perhaps a generic table like 'views' could be used
> with the GenericForeignKey setup that Django uses (like for "tags" or
> "comments"). This could possibly allow queries on that model to be
> cached (unless the query is hitting the "views" table in a join for
> every object load/hit).
>
> Even if no cache benefit can be gained, I like the ability to add a
> "views" property to ANY object that you please just by setting up a
> Generic foreign key though, it's much easier to add that functionality
> to any object without having to modify your DB schema at all.
>
> Jay
>
> [EMAIL PROTECTED] wrote:
> > Any suggestions on how I could track how many times an object has
> > loaded and/or been clicked?
> >
> > I mean, obviously in my model I'd need something like
> >
> > class Item(models.Model):
> > views = IntegerField()
> >
> >
> > but how would I update views when Item was loaded on a page?
> >
> >
> > >
> >
> >
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---