Skip to content

An abstract model in Django that supports store likes and denormalized like count.

License

Notifications You must be signed in to change notification settings

linghu-chong/django-likable-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-likable-model

An abstract model in Django that supports store likes and denormalized like count.

What is likable model?

If user like a post or a comment. We need to create a like table to store who like what, so that we can avoid the same user contributing too much likes on the same item.

Also in the Post or Comment model, we need to store a denormalized field like_count to denote how many users liked this item. Why not just select count from like table? Because select count is heavy, if you store a denormalized field in the model, it's much more faster.

How to use it?

Say you want to make Post model likable.

The orignal model may looks like:

class Post(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=255)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Change it to:

from likable.models import Likable

class Post(Likable):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=255)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

When user liked a post:

post = Post.objects.get(id=post_id)
post.like_by(user)

When user canceled a like:

post.like_canceled_by(user)

Get how many users liked a post:

post.like_count

Get most recent likes / users

likes = post.get_recent_likes(k=10)
users = [like.user for like in likes]

About

An abstract model in Django that supports store likes and denormalized like count.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages