I have a fairly complex query that I’ve been trying to create using Django’s query abstraction language, but so far no luck.
I have 2 tables I need to query one of which is auth_user. The other table I’ll call contact. The contact table has two columns each of which are foreign keys to the auth_user table (user_id, user_accept_id). The contact table establishes relationships between people on the website.
What I need to build is a query that retrieves people I am NOT connected to.
Here is the equivalent query in raw sql, my id will be 1 for the sake of the query below.
select u.id, u.username, resultset.id, resultset.myid, resultset.cid
from auth_user as u
left join (
select id, user_id as myid, user_accept_id as cid from polls_contacts where user_id=1
select id, user_accept_id as myid, user_id as cid from polls_contacts where user_accept_id=1
group by myid, cid
) as resultset on u.id=resultset.cid
u.id != 1 and resultset.id IS NULL;
I’m considering just using the method to write this query as raw, but would rather find out how to use the abstraction layer, if possible.