0

how can I execute such query in django:

SELECT * FROM keywords_keyword WHERE id not in (SELECT keyword_id FROM sites_pagekeyword)

In the latest SVN release we can use:

keywords = Keyword.objects.raw('SELECT * FROM keywords_keyword WHERE id not in (SELECT keyword_id FROM sites_pagekeyword)')

But RawQuerySet doesn't support filter(), count(), indexing and other things. Is there another way?

2 Answers 2

8

Keyword.objects.exclude(id__in=PageKeyword.objects.all()

Keyword.objects.exclude(id__in=PageKeyword.objects.values('keyword_id'))

For future reference, exclude is documented here.


Edit: Yes, you are right; I corrected my answer. See above.


Edit: Even more readable:

Keyword.objects.exclude(pagekeyword__in=PageKeyword.objects.all())
Sign up to request clarification or add additional context in comments.

Comments

1

I've tested your code and it works not as expected, here is the right solution for my task:

Keyword.objects.exclude(id__in=PageKeyword.objects.values('keyword_id'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.