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
to get followers count
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):in the userprofile class append these functions to the end of the models
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)
def get_followers(self):then to use it in the template
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
to get followers count
{{ user.profile.get_follower_count }}
Comments
Post a Comment