django ManyToMany through help

Posted by dotty on Stack Overflow See other posts from Stack Overflow or by dotty
Published on 2010-03-15T11:38:52Z Indexed on 2010/03/15 11:49 UTC
Read the original article Hit count: 535

Filed under:
|
|

Hay I've got a question about relationships.

I want to Users to have Friendships. So a User can be a friend with another User. I'm assuming i'll need to use the ManyToManyField, through a Friendship table. But i cannot get it to work. Any ideas?

Here are my models.

class User(models.Model):
    username = models.CharField(max_length=999)
    password = models.CharField(max_length=999)
    created_on = models.DateField(auto_now = False, auto_now_add = True)
    updated_on = models.DateField(auto_now = True, auto_now_add = False)
    friends = models.ManyToManyField('User', through='Friendship')

class Friendship(models.Model):
    user = models.ForeignKey('User')
    friend = models.ForeignKey('User')

Thanks

© Stack Overflow or respective owner

Related posts about many-to-many

Related posts about django