Using a many-to-many through with users

6 views
Skip to first unread message

Charlie L

unread,
Mar 17, 2010, 4:44:44 PM3/17/10
to Django users
I'm running into some trouble adding a custom many-to-many field to
the User model.

http://dpaste.com/173041/

I am modelling social relationships, and to do this I have a
relationship model with 2 foreign keys to users, a 'from_user' and a
'to_user' -- in essence, this looks like a Many-to-many table between
users and can probably act like one.

So what I'm trying to do is, mimicking django's
ManyRelatedObjectsDescriptor, create a RelatedManager dynamically that
will access the relationship table.

It's almost working right, except for the last part of the WHERE query
that's being generated:

In [5]: co = User.objects.get(username='coleifer')

In [6]: co.relationships.all()
Out[6]: []

In [8]: connection.queries.pop()
Out[8]:
SELECT "auth_user"."id", etc, etc
FROM "auth_user"
INNER JOIN "relationships_relationship"
ON ("auth_user"."id" = "relationships_relationship"."from_user_id")
WHERE "relationships_relationship"."id" = 69327
LIMIT 21


the where should read "relationships_relationshpi"."to_user_id" =
69327

Charlie L

unread,
Mar 17, 2010, 4:48:11 PM3/17/10
to Django users
I guess I should have made this more clear what my question was:

Anybody know how to fix the WHERE clause? Is there a better way to
achieve this functionality?

Reply all
Reply to author
Forward
0 new messages