Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django friends as many-to-many field - better storing User or UserProfile (self) in field?

I found two ways of implementing a symmetrical friendship system (you are my friend, so I am also your friend) in Django:

As suggested inside the docs:

class UserProfile(models.Model):
    friends = models.ManyToManyField(User, related_name='friends')

Now, I'd like to get all "friendly" user AND userprofile models with one select_related-query like so (that should be a reverse joined lookup):

profiles = UserProfile.objects.filter(user__in=request.user.get_profile().friends.all()).select_related()

I query the userprofile, because that way, I can use select_related() and all related objects are cached.

On the other hand, I can define my model referencing the friends field to "self", like so:

class UserProfile(models.Model):
    friends = models.ManyToManyField('self')

Now, my select_related friend lookup looks like that:

profiles = this_user.get_profile().friends.all().select_related()

I always need both, the user object and it's related profile: The second approach is much simpler concerning the reversed lookup via select_related(), but does practically the same. However, by using "self" as field reference, Django handles for me the symmetrical friendship. Thus, I don't have to create two entries per friendship in the database manually. Django does that for me. The symmetrical option, however, does only work on "self" referencing fields.

Which is the better solution? I can't find anything about it. Any ideas appreciated - thanks!

like image 348
Simon Steinberger Avatar asked Oct 25 '11 09:10

Simon Steinberger


1 Answers

Simon,

Sometimes its better to use an existing plugin, rather than trying to re-invent the wheel.

Have you had a look at django-friends?

like image 118
ApPeL Avatar answered Nov 15 '22 04:11

ApPeL