0

I have some problems with my query - with filter() it's ok but with exclude() doesn't work. My models:

class Dictionary(DateTimeModel):
    base_word = models.ForeignKey(BaseDictionary, related_name=_('dict_words'))
    word = models.CharField(max_length=64)
    version = models.ForeignKey(Version)

class FrequencyData(DateTimeModel):
    word = models.ForeignKey(Dictionary, related_name=_('frequency_data'))
    count = models.BigIntegerField(null=True, blank=True)
    source = models.ForeignKey(Source, related_name=_('frequency_data'), null=True, blank=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name=_('frequency_data'))
    user_ip_address = models.GenericIPAddressField(null=True, blank=True)
    date_of_checking = models.DateTimeField(null=True, blank=True)
    is_checked = models.BooleanField(default=False)

And I want to get some words from Dictionary where whose frequencies are not from some user

Dictionary.objects.prefetch_related('frequency_data').filter(frequency_data__user=1)[:100] - OK

Dictionary.objects.prefetch_related('frequency_data').exclude(frequency_data__user=1)[:100] - processor up to 100% and loading

Without prefetch_related the same. What is wrong with exclude?

EDIT Dictionary db tabel - 120k rows FrequencyData - 160k rows

EDIT2 psql(9.6.6)

14
  • please show Dictionary.objects.prefetch_related('frequency_data').exclude(frequency_data__user=1).count() Commented May 30, 2018 at 12:00
  • This same - still waiting... Dictionary table has about 120k records and FrequencyData 160k. I check with filter() and count() - it was 120k - so exclude should show 0 Commented May 30, 2018 at 12:10
  • try qs=Dictionary.objects.prefetch_related('frequency_data').exclude(frequency_data__user=1).count(); print(qs.query) and then in the ./manage.py dbshell try to execute the result of print. Commented May 30, 2018 at 12:21
  • For this request shell the same like above - it's hangs. I try it locally and get AttributeError: 'int' object has no attribute 'query' Next I try locally: from django.db import connection qs=Dictionary.objects.prefetch_related('frequency_data').exclude(frequency_data__user=1).count() print(connection.queries) and my result: pastebin.com/ZMG0StN1 Commented May 30, 2018 at 12:53
  • cool but why there 3 times query? Commented May 30, 2018 at 13:01

0

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.