On Fri, Aug 9, 2013 at 9:38 AM, Andy McKay <[email protected]> wrote:

> On Thu, Aug 8, 2013 at 12:41 PM, Robin Lery <[email protected]> wrote:
>
>> How do i create a Friendship model? Please guide me.
>>
>
> You would need to provide more detail to get much help. The tutorial
> covers how to make models:
>
> https://docs.djangoproject.com/en/dev/intro/tutorial01/
>
>
>



I have implemented Follower and Following model for my project.
Take a look a the this follower/followee relationship :

class Follow(models.Model):
>     """ Model to represent Following relationships """
>     follower = models.ForeignKey(User, related_name='following')
>     followee = models.ForeignKey(User, related_name='followers')
>     created = models.DateTimeField(default=timezone.now)
>
>     class Meta:
>         verbose_name = _('Following Relationship')
>         verbose_name_plural = _('Following Relationships')
>         unique_together = ('follower', 'followee')
>
>     def __unicode__(self):
>         return "User %s follows %s" % (self.follower, self.followee)
>
>     def save(self, *args, **kwargs):
>         # Ensure users can't be friends with themselves
>         if self.follower == self.followee:
>             raise ValidationError("Users cannot follow themselves.")
>         super(Follow, self).save(*args, **kwargs)
>


If you hook this model to admin.py , you can add or remove followers to
particular user.
I use Tastypie to do these Operations [1].
Friendship would be represented with a similar model , but since Friendship
is bilateral (ie: you would need a way to add a model manager).
What I mean to say , the Social Relation Model like above will hold your
relations.
Apart from this you would need a way to send friend request , and when user
approves to request then only this model will be saved.
I think following code will be helpful for you [2] .



[1] -
https://github.com/aregee/moksaya/blob/master/profiles/api.py#L177-L197

[2] -
https://github.com/revsys/django-friendship/blob/master/friendship/models.py#L125-L296





-- 
-------------------------------------------------------------------------------------------------------
Rahul Gaur
irc : iamaregee2
Web: http://www.rahulgaur.info
Github: https://github.com/aregee

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to