Why is exclude() different than difference()?

20 views
Skip to first unread message

vipe...@gmail.com

unread,
Feb 7, 2018, 2:43:37 AM2/7/18
to Django users
Hi,

I have the following models: Abonnent -1:n- Sendungsadresse -1:1- Redresse
Model `Abonnent` has 99681 rows.

This query returns 829 rows, as expected:
    `Abonnent.objects.filter(sendungsadresse__redresse__isnull=False, sendungsadresse__redresse__korrigiert_am__isnull=True).distinct()`

The second query returns 98852 rows, as expected:
    `Abonnent.objects.all().difference(Abonnent.objects.filter(sendungsadresse__redresse__isnull=False, sendungsadresse__redresse__korrigiert_am__isnull=True).distinct())`

But I want to be able to keep filtering and that does not work with difference().
So I created this query, that returns 98851 rows, when I was expecting 99852 rows:
    `Abonnent.objects.exclude(sendungsadresse__redresse__isnull=False, sendungsadresse__redresse__korrigiert_am__isnull=True)`

The missing Abonnent object in that query has two Sendungsadresse objects. The following statements are true for each corresponding Sendungsadresse object, but neither of them should be excluded, unless I am missing some peculiarity about exclude() (which I probably am, to be honest):
    Object 1: redresse__isnull => True,  redresse__korrigiert_am__isnull => True
    Object 2: redresse__isnull => False, redresse__korrigiert_am__isnull => False
Reply all
Reply to author
Forward
0 new messages