2

I have word model and phrase model

class Word(models.Model): 
        checked = models.BooleanField(default=False)

class Phrase(models.Model):
    words = models.ManyToManyField(Word, null=True,related_name = "phrases")

Word model has attribute checked and many to many connection to phrase

I have a query, something like this:

Phrase.objects.exclude(words__checked=True).filter(words__group__pk__in = groups_ids)

But it works really really slow on big datasets, and the problem is in exclude section, cause without it - it works fast enough So I found a suggestion that I should use raw sql here, Performance issue with django exclude

So, how can rewrite this sentence with raw sql ?

(I need this query to both work postgres and mysql due to requirements, or I will need two queries if one query can't achieve this, postgres query has more priority)

So far I 've tried to use .extra syntax,but it didn't work, so asking it here.

user2950593
  • 9,233
  • 15
  • 67
  • 131
  • Did you try add `word__checked=False` to the filter? – arcegk Aug 08 '18 at 19:53
  • 1
    @arcegk: the two are *not* the same. A filter means that you will JOIN, and simply obtain the `Phrase`s for which there is at least one `word_checked=False`, whereas `.exclude(word__checked=True)` will excude the *`Phrase`* from the moment *one* of the `word__checked=True`. – Willem Van Onsem Aug 08 '18 at 20:08

0 Answers0