Skip to content

Instantly share code, notes, and snippets.

@renjithsraj
Last active November 7, 2019 03:37
Show Gist options
  • Save renjithsraj/dc98bb7cf95d2dc049f2468981cf3d5e to your computer and use it in GitHub Desktop.
Save renjithsraj/dc98bb7cf95d2dc049f2468981cf3d5e to your computer and use it in GitHub Desktop.
from django.contrib.auth.models import User
class Artist(User):
name = models.CharField(max_length=120, verbose_name='name')
class Followers(models.Model):
artist = models.ForeignKey(Artist, related_name='following')
follower = models.ForeignKey(Artist, related_name='followers')
date_created = models.DateTimeField(auto_now_add=True, verbose_name='Date Created')
date_updated = models.DateTimeField(null=True, blank=True)
is_follow = models.BooleanField(default=False, verbose_name='Followed')
class Meta:
verbose_name = "Followers"
verbose_name_plural = "Followers"
Here I'm trying to show you the sample demonstartion for the following functionality.
The Artist and Followers are models.
Artist is Inherited from the django user model
You can see the Followers models Artist is fk of both artist and follower.
Why i Add this :
Suppose Im a artist ---- im following you....
so the method is
artist = Artist.objects.get(id=request.user.id) ### return my id
you = Artist.objects.get(id= 12313133) ### return your id
Scenario 1:
Im following you/you following me also (--> <--)
follow = Followers.objects.create(artist=artist, follower= you,is_follow=True)
Scenario 2 :
get followers
=============
in Followers models i have added related names for the fk of Artist "following" and "followers".
so get the instance for the Artist.
queryset = artist.followers.all() ### return all the followers list
get following users
===================
queryset = artist.following.all() ### return all the following users list
Scenario 3 :
un follow ---> is_follow make True or False for this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment