Follower/Following database logic with django

I have used a lot of social networking sites, including twitter and instagram. Sometimes i wonder how this FOLLOW concept work in these applications. I have done a bit of investigation and finally came up with a way to do that in django.

So here it goes ….

The idea is We have Auth user in django. So everytime we create an user with our auth user a new record is inserted into Users table, and i have created a new model for storing some other data for the user which is not provided by django. like date of birth, age, sex, phone number, follower and following count. But we dont have any way here to follow/unfollow other users. After a lot of thoughts i found this. The users will follow other users in the application. That means our Users table will have ForeignKey to itselt(to the same table). More like below….
Here in this diagram User1 is following User3, User4 and User6. Whereas User2 is getting followed by User5, User6 and User7. So as you can see in this diagram it is basically a many to many relation to the same table. Like every other many to many relation here also we need an intermediate mapper that will map the ForeignKey relation between the Users. So the simplified outcome will be something like below.
So it is basically the same thing as the previous image but a bit simplified. It has relation with the same table through a mapper and the output block determines how to show the relation.

Here come the code and explanation of the above.
For better understanding of project i will suggest to start a new django app called sitemanager and write all user manager related code there only. But here comes the problem. django does not support the ForeignKey to same table more than once in the same model.

So how do we do it? The solution is here.
We use a key word called “related_name” in the attribute so that the model will understand that this is the same ForeignKey used for two different purposes. Still we have a problem. we give 2 diffrent related names as followed and following and it should work but it wont. The rule is (what i have figured out) you should give a combination of words in related names, both must be different but there first word should be same and followed by underscore and other words.

In the model file write the following, and we are done. we have a table that can hold all the following and follower information. Not only this we can access all user data from the selected object using the related name in the querySet. I will explain that in my next blog.

class Follow(models.Model):
      following = models.ForeignKey(User, related_name="who_follows")
      follower = models.ForeignKey(User, related_name="who_is_followed")
      follow_time = models.DateTimeField(auto_now=True)

      def __unicode__(self):
          return str(self.follow_time)

Once you write this you are all set to go 😀 just have a follow button, write a few javascript and and do operations on it and done.

If you have any queries please comment here. If you like my blog subscribe and if you want to get in touch follow me on twitter 😀 my handle  is (@binayray2009)
or connect with me on linked in (
add me on g+ (
or in facebook (

Thank you for your interest 🙂

4 thoughts on “Follower/Following database logic with django

  1. Hi Binay! thanks for the awesome and good tutorial. Much appreciated. I just keep hanging at the part of making this whole thing work. Could you describe how the view will look? Greetings from The Netherlands

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s