In this article we are going to create a simple generic django likes app named Django Liked that can be used to like any model defined in your django project. To create this app we are going to use django’s contenttype framework. So without further ado let’s jump right into the code. First thing we’d want to do is to create a new django project and a virtual environment to work in.

Django Likes App


The above commands are going to create a new virtual environment, Install the latest version of django (Django 1.10 as of this writing) and create a new project named liked. This is going to create a folder named liked in your current directory that contains our django likes app. the structure of the directory should look like the following.

To keep things clean and understandable, I usually change the lower level directory name to config as it contains mostly the configuration files for your django project. Once you change the name of the directory make sure you also update the, and file to tell django about the changes you just made. So let’s go ahead and do that now.

Update the file

Similarly update the file

Now update the ROOT_URLCONF setting in your settings file.

Now we want to create a new app called liked define our Like model.

In the file add the following code.

And believe it or not. That’s it, Our likes app is ready to be used in any of the django projects. But before we move on to the usage of the app, I would create a pip package for our app for easy use in the future and also to make it available to fellow developers. You may want to have a look at our article How to create pip packages.

Using the django liked app

Now we can use the django liked app in any projects that we like. First we’d need to install the app using.

Now we would require to add the liked app to our INSTALLED_APPS, so open your file and update it there.

The last step is to update the database schema. So let’s run the following migration command

That’s it. Let’s create a like object for one of our models.

To get all the likes for a particular object you can use a GenericRelation field in its model. For example.

and now get the like count for an article with id 1 as follows:

Please feel free to ask any questions that you may have and share this article with fellow developers. Also This app is available to use under MIT License. Following are a few useful links.


Source Code

PIP Package