Snippet taken from this question
from django.db.models import F
...
MyModel.objects.filter(id=...).update(hit_count=F(hit_count)+1)
It was suggested to put in the middleware and I read up a bit on middleware but would greatly appreciate if someone could point out what they would do in this situation. Using my Bug
model as an example it has a unique slug field and the pk.
Here's my model:
class Bug( models.Model ):
name = models.CharField( max_length=100 )
slug = models.SlugField(unique=True)
excerpt = models.TextField()
excerpt_markdown = models.TextField( editable=False, blank=True )
summary = models.TextField()
summary_markdown = models.TextField(editable=False, blank=True)
#workaround = models.TextField()
#workaround_markdown = models.TextField(editable=False, blank=True)
date_added = models.DateTimeField()
poster = models.ForeignKey(User)
tags_string = TagField()
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
def get_absolute_url(self):
return '/bugs/%s/' % self.slug
def save( self, force_insert=False, force_update=False ):
self.summary_markdown = markdown( self.summary )
self.excerpt_markdown = markdown ( self.excerpt )
#self.workaround_markdown = markdown( self.workaround )
super( Bug, self ).save( force_insert, force_update )
Links are viewed through /bugs/(slug)
. I've yet to add the new column but I imagine it's just hit_counter = models.IntegerField()