Last active
October 27, 2020 11:22
-
-
Save mrob11/9607834 to your computer and use it in GitHub Desktop.
Simple follower/following relationship in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./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', '[email protected]', 'password') | |
>>> paul = User.objects.create_user('paul', '[email protected]', 'password') | |
>>> george = User.objects.create_user('george', '[email protected]', 'password') | |
>>> ringo = User.objects.create_user('ringo', '[email protected]', '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() | |
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Yeah that looks pretty clean. No pun intended.
Hi Mike, why did you choose to use ForeignKey instead of ManyToMany Relationship?
A user should be able to follow multiple users and multiple users should also be able to follow a user. Is using a foreignkey not going to restrict it to just one user being able to follow multiple users and multiple users not being able to follow a user...or vice versa?
@Olamidun if I chose a ManyToMany field, that would have to have been implemented directly on the User model which is an unnecessary complication. This method is essentially a through
model on a M2M field without declaring it that way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be a one way