Django Contenttypes and Decorator

The site uses 2 objects - articles and blogs. Each time an article or blog is viewed, the associated counter should increase by one.

The idea is to have a "top ten" application that measures the "popularity" of articles and posts.

Since I am using more than one object, I would like the Tracker model to use genericForeignKey for related objects.

#models.py
class Tracker(models.Model):
    count = models.PositiveIntegerField(default=1)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    def hit(self):
        self.count += 1

      

I would like to write a decorator that wraps the view function, but this may not be necessary.

thanks

+1


a source to share


1 answer


If I understand you correctly, you want to count every copy of every object. I would do it using post_init signal - if you don't mind it's not a decorator.

Here is the code I wrote - using post_save instead of post_init:



def thumb_init(sender, **kwargs):
    kwargs['instance'].process()
    kwargs['instance'].make_thumbnail()

post_init.connect(thumb_init, sender=Thumbnail) 
post_init.connect(thumb_init, sender=<otherModel here>)  

      

+2


a source







All Articles