Skip to content

Instantly share code, notes, and snippets.

@mike360 mike360/models.py
Last active Apr 9, 2020

Embed
What would you like to do?
Simple follower/following relationship in Django
$ ./manage.py shell
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from followers.models import *
>>> john = User.objects.create_user('john', 'lennon@thebeatles.com', 'password')
>>> paul = User.objects.create_user('paul', 'mccartney@thebeatles.com', 'password')
>>> george = User.objects.create_user('george', 'harrison@thebeatles.com', 'password')
>>> ringo = User.objects.create_user('ringo', 'starr@thebeatles.com', 'password')
>>> john.following.add(Follower(following=paul))
>>> john.following.add(Follower(following=george))
>>> paul.following.add(Follower(following=john))
>>> ringo.following.add(Follower(following=john))
>>> ringo.following.add(Follower(following=paul))
>>> ringo.following.add(Follower(following=george))
>>> john.followers.all()
[<Follower: paul follows john>, <Follower: ringo follows john>]
>>> paul.followers.all()
[<Follower: john follows paul>, <Follower: ringo follows paul>]
>>> george.followers.all()
[<Follower: john follows george>, <Follower: ringo follows george>]
>>> john.following.all()
[<Follower: john follows paul>, <Follower: john follows george>]
>>> ringo.following.all()
[<Follower: ringo follows john>, <Follower: ringo follows paul>, <Follower: ringo follows george>]
>>> ringo.followers.all()
[]
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class Follower(models.Model):
follower = models.ForeignKey(User, related_name='following')
following = models.ForeignKey(User, related_name='followers')
class Meta:
unique_together = ('follower', 'following')
def __unicode__(self):
return u'%s follows %s' % (self.follower, self.following)
@coci

This comment has been minimized.

Copy link

coci commented May 22, 2019

what is the following in john.following.add(.....) ?

@Elbekgithub

This comment has been minimized.

Copy link

Elbekgithub commented Feb 21, 2020

very good

@Elbekgithub

This comment has been minimized.

Copy link

Elbekgithub commented Feb 21, 2020

dwwdw

@kushagra219

This comment has been minimized.

Copy link

kushagra219 commented Mar 4, 2020

How can we ensure that follower and following are not same ??
Like John follows John doesn't make any sense?

@abd-koumare

This comment has been minimized.

Copy link

abd-koumare commented Mar 17, 2020

I think you can achieve that programmatically, you just have to check if the user that will follow John is not John itself

@abd-koumare

This comment has been minimized.

Copy link

abd-koumare commented Mar 17, 2020

I'm currently working on something like that if someone has a better solution I'm interested

@abd-koumare

This comment has been minimized.

Copy link

abd-koumare commented Mar 17, 2020

what is the following in john.following.add(.....) ?

django ORM provides some attributes and methods we don't have to use it if it makes no sense

@abd-koumare

This comment has been minimized.

Copy link

abd-koumare commented Mar 17, 2020

Follower.objects.create(follower=foo, following=bar)

@phongnm-0776

This comment has been minimized.

Copy link

phongnm-0776 commented Apr 3, 2020

Im new to django, but why the class name is Follower? Why not something like ProfileRelation or anything else different? Sr if my question doen't make sense.

@mike360

This comment has been minimized.

Copy link
Owner Author

mike360 commented Apr 6, 2020

Im new to django, but why the class name is Follower? Why not something like ProfileRelation or anything else different? Sr if my question doen't make sense.

You can name it whatever you like. When I wrote this nearly 7 years ago it was just a very quick and dirty example to illustrate a many-to-many relationship between users. I was just quickly answering someone's question on Reddit I think. It seems to have gained some attention lately for some reason.

@mike360

This comment has been minimized.

Copy link
Owner Author

mike360 commented Apr 6, 2020

How can we ensure that follower and following are not same ??
Like John follows John doesn't make any sense?

I would put a check in the Follower.save method to make sure follower and following are not the same user.

Edit: Actually, it's probably better to put that type of validation in a Model Form, or the Model's clean method rather than the Model save method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.