I agree with Kenneth. Your models seem odd. Why not use a ManyToManyField? The django docs even provides quite a nice example (see: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.symmetrical):
class Person(models.Model): friends = models.ManyToManyField("self") You could adapt this to your UserProfile I suppose. -- Jon Black www.jonblack.org On Tue, Jul 3, 2012, at 12:57, kenneth gonsalves wrote: > 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+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.