Django retrieving users from tuples -
auth_user_model = getattr(settings, 'auth_user_model', 'auth.user') class friend(models.model): """ model represent friendships """ to_user = models.foreignkey(auth_user_model, related_name='friends') from_user = models.foreignkey(auth_user_model, related_name='_unused_friend_relation') created = models.datetimefield(default=timezone.now) objects = friendshipmanager() class meta: verbose_name = _('friend') verbose_name_plural = _('friends') unique_together = ('from_user', 'to_user') def __unicode__(self): return "user #%d friends #%d" % (self.to_user_id, self.from_user_id) class friendshipmanager(models.manager): def unread_requests(self, user): """ return list of unread friendship requests """ key = cache_key('unread_requests', user.pk) unread_requests = cache.get(key) if unread_requests none: qs = friendshiprequest.objects.select_related(depth=1).filter( to_user=user, viewed__isnull=true).all() unread_requests = list(qs) cache.set(key, unread_requests) return unread_requests class person(models.model): user = models.foreignkey(user)) name = models.charfield(max_length=30, blank=true) image = models.imagefield(upload_to='photos', blank=true) def __unicode__(self): return self.user.username
i using friendship model . model allow create friends between users. have userprofile model called profile above .
the below command shows how view friendship request particular user
>>> user = user.objects.get(pk=1) >>> user1 = user.objects.get(pk=1) >>> user2 = user.objects.get(pk=2) >>> user3 = user.objects.get(pk=3) >>> friend.objects.add_friend(user1,user3) <friendshiprequest: user #1 friendship requested #3> >>> friend.objects.add_friend(user2,user3) <friendshiprequest: user #2 friendship requested #3> >>> requests = friend.objects.unread_requests(user=user3) >>> requests [<friendshiprequest: user #1 friendship requested #3>, <friendshiprequest: user #2 friendship requested #3>] >>> requests[0].to_user <user: hello>
i'm trying show friendship request particular user images but
i can't @ moment because requests in tuples ( below example) .
i want able retrieve users requested friendship particular user
and use users retrieve users userprofile model referring person can show images in template
in it's simplest form
how can retrieve users request friendship ( below example can view user name of one
>>> requests = friend.objects.unread_requests(user=user3) >>> requests [<friendshiprequest: user #1 friendship requested #3>, <friendshiprequest: user #2 friendship requested #3>] >>> requests[0].to_user
and collect userprofile ( person )
person.objects.filter ( ? )
create list of friends
list_of_friend_requests = [r.to_user r in requests]
filter using django's filtering
persons = person.objects.filter(user__in=list_of_friend_requests)
Comments
Post a Comment