Skip to main content

How To Make A Django Follower System

In a recent project i worked on, i needed a way to implement a follower system, after searching the net and not getting any concrete information on making a follower system. i decided to write my own after i finish making the system on my own.
So lets move forward, i assume that you already have a django project started and you are using a custom profile class
open the models.py file of the login/authentication app and paste the below

class Followers(models.Model):
follower = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='follower')
 following = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='following')


def __str__(self):
        return '{} followers {}'.format(self.follower, self.following)
in the userprofile class append these functions to the end of the models

    def get_followers(self):
        get_follower = Followers.objects.filter(follower__pk=self.user.pk)
        followers = []
        for followers in get_follower:
            followers.append(followers.follower)
        return followers

    def get_followers_count(self):
        followers_count = Followers.objects.filter(followers__pk=self.user.pk).count()
        return followers_count


def get_following(self):
        get_following = Followers.objects.filter(following__pk=self.user.pk)
        followings = []
        for followings in get_following:
            followers.append(followings.following)
        return following

    def get_followings_count(self):
        following_count = Followers.objects.filter(following__pk=self.user.pk).count()
        return following_count
then to use it in the template
to get followers count
{{ user.profile.get_follower_count   }}


Comments

Popular posts from this blog

Tutorial 1 - How to Use uuid in Django

uuid is used when you need unique values from a model e.q (twitter with their status id) in the models.py id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) #if you dont want to use the uuid as the model id   use #unique_id = models.UUIDField(default=uuid.uuid4, editable = False,unique=True) in urls.py from django.conf.urls import url from . import views urlpatterns = [     url(r'^status/$', views.status, name='status'), ] and from views.py from django.shortcuts import render def status(request, uuid):     status = get_object_or_404(id=uuid)     return render(request, 'core/status.html', {'status': status})

How To Install Qt5.* AND QtCreator On UBUNTU 16.04

This tutorial was done with the following software versions: Ubuntu 16.04 LTS Qt Creator 3.5.1 Qt 5.5.1 Install Qt sudo apt-get install build-essential sudo apt-get install qt5-default   sudo apt-get install qtcreator Install documentation and examples If Qt Creator is installed thanks to the Ubuntu Software Center or thanks to the synaptic package manager, documentation for Qt Creator is not installed. Hitting the F1 key will show you the following message : "No documentation available". This can easily be solved by installing the Qt documentation: sudo apt-get install qt5-doc And eventually: sudo apt-get install qt5-doc-html qtbase5-doc-html If you the examples are also missing: sudo apt-get install qtbase5-examples Restart Qt Creator to make the documentation available.

Python Snippet 01 - Requirements.txt

Recently i needed to get the requirements of my Django project so i could move it to a production server.  So i researched the web and found REQUIREMENTS.TXT Why requirements.txt? Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once. Format for requirements file:  Django ==0.10.1 djangorestframework ==1.2.0 Method 1: $ pip freeze > requirements.txt Use pip’s freeze command to generate a requirements.txt file for your project: If you save this in requirements.txt, then you can $ pip install -r requirements.txt to install all the requirements of your project. NB: Use the pip freeze command in your virtualenv or else yuou will list all your dependencies