views:

32

answers:

0

Hi!

I have the following model:

class MyUser(User):
    # some fields...
    contact = models.ManyToManyField("self", through='Contact', symmetrical=False, related_name="contact_set" )


class Contact(models.Model):
    user1 = models.ForeignKey( MyUser, related_name="contact_set1")
    user2 = models.ForeignKey( MyUser, related_name="contact_set2")
    confirmed = models.BooleanField()

and this view to create a contact

def add_contact( request, username=None ):
    if username:
        try:
            user = MyUser.objects.get(username=username)
        except User.DoesNotExist:
            user = None

    if user:
        contact = Contact( user1 = MyUser.objects.get(pk=request.user.id), user2=user, confirmed=False )
        contact.save()
        return render_to_response( "accounts/add_contact.html", {'user': user,} )
    else:
        return HttpResponseRedirect("/home")

def list_contacts( request, username=None ):
    if username:
        try:
            user = MyUser.objects.get(username=username)
        except User.DoesNotExist:
            user = None
    else:
        user = MyUser.objects.get(pk=request.user.id)

    if user:
        contacts = user.contacts.all()
        return render_to_response("accounts/list_contacts.html", {'contacts': contacts,} )
    else:
        return HttpResponseRedirect("/home")

Ok now... the code should be verbose enough, so I'll explain it briefly: Social networking, users add others to their contacts ("friends", "buddies", however you call it). If user A adds user B, B is also in contact with A...

The code works... but only one-way. If I'm logged in as user A and want to add user B to my contacts, B will show up on my contact list, but not the other way around. But I want also to show up on B's contact list - it should make no difference, who added whom. How may I manage that?