Tuesday, July 3, 2012

Re: Can't figure out how to obtain organized list of my friends

On Mon, 2012-07-02 at 20:31 -0700, Keith D. Cardin wrote:
> I'm trying to create a "my friends" page, which will list the user's
> friends listed in alphabetical order.
> I have two classes involved with this data: Profile [and] Friendship
>
> *class Friendship(models.Model):*
> friend = models.ForeignKey(User,
> related_name='friend1')
> friendwith = models.ForeignKey(User,
> related_name='friend2')
>
> *class Profile(models.Model):*
> user = models.OneToOneField(User,
> unique=True,
> verbose_name=_('user'),
> related_name='profile')
> first_name = models.TextField(max_length=50)
> last_name = models.TextField(max_length=50)
>
> The exact raw SQL query [successfully tested] is as follows::
> *select profiles_profile.first_name,profiles_profile.last_name FROM
> profiles_profile, friends_friendship WHERE profiles_profile.user_id =
> friends_friendship.friendwith_id AND friends_friendship.friend_id =
> 30
> ORDER BY first_name ASC;*
> *
> *
> My problem is I'm not sure how to do this in Python.
> '30' is the user's id/pk.

to get a list of friends of a particular user this may work:

first get the user:
me = Profile.objects.get(user = myuser)

then get the friend1
friends = me.fiiend1_set.all()

the confusing thing about your models is that it seems to me that your
Friendship model should be split into two models.
--
regards
Kenneth Gonsalves

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment