Jason
Jason

Reputation: 11363

Django - how to exclude results from existing querysets in following model queries?

I have two models: Peers and Competitors, which are related to a Client model. All have a basic ID field.

Lets say I execute a *.objects.filter(id = some_id) query on Peers and Competitors

Rather than writing my own loops, is there a built in way for Django to allow me to get all Client objects that do not exist in the Peer and Competitor querysets?

Upvotes: 0

Views: 328

Answers (1)

J. Ghyllebert
J. Ghyllebert

Reputation: 2027

You could use a reverse relation:

clients = Client.objects.exclude(peer__client=client_id) 
clients = clients.exclude(competitor__client=client_id)

I assume your Foreign key is named client in both Peer and Competitor models.

Upvotes: 2

Related Questions