[Django] #34589: exclude does not support nested ForeignKey relationship

25 views
Skip to first unread message

Django

unread,
May 22, 2023, 7:35:45 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I need to find trips that are not
trip__method__title=ShippingChoice.ORGANIZATION
my code is like this:
working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
],
trip__method__title=ShippingChoice.ORGANIZATION
).filter(
updated_at__lt=time_threshold,
)

I forced to use a big list on filter instead to use the exclude. my code
is like this:

working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
]
).filter(
updated_at__lt=time_threshold,
trip__method__title__in=(ShippingChoice.ALO, ShippingChoice.SNAP,
ShippingChoice.MIAREH)
)

--
Ticket URL: <https://code.djangoproject.com/ticket/34589>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 22, 2023, 7:42:39 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Old description:

> I need to find trips that are not
> trip__method__title=ShippingChoice.ORGANIZATION
> my code is like this:
> working_shipments = Shipment.objects.exclude(
> state__in=[
> ShipmentStateChoices.CANCELLED,
> ShipmentStateChoices.DELIVERED
> ],
> trip__method__title=ShippingChoice.ORGANIZATION
> ).filter(
> updated_at__lt=time_threshold,
> )
>
> I forced to use a big list on filter instead to use the exclude. my code
> is like this:
>
> working_shipments = Shipment.objects.exclude(
> state__in=[
> ShipmentStateChoices.CANCELLED,
> ShipmentStateChoices.DELIVERED
> ]
> ).filter(
> updated_at__lt=time_threshold,
> trip__method__title__in=(ShippingChoice.ALO,
> ShippingChoice.SNAP, ShippingChoice.MIAREH)
> )

New description:

I need to find trips that are not
`trip__method__title=ShippingChoice.ORGANIZATION`
my code is like this:

{{{
working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
],
trip__method__title=ShippingChoice.ORGANIZATION
).filter(
updated_at__lt=time_threshold,
)
}}}

I forced to use a big list on filter instead to use the exclude. my code
is like this:

{{{
working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
]
).filter(
updated_at__lt=time_threshold,
trip__method__title__in=(ShippingChoice.ALO, ShippingChoice.SNAP,
ShippingChoice.MIAREH)
)

}}}

--

Comment (by David Sanders):

formatted description

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:1>

Django

unread,
May 22, 2023, 7:49:20 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

It's not clear to me what you're trying to report, however, it appears to
be a support question. If you're having trouble understanding how Django
works, see TicketClosingReasons/UseSupportChannels for ways to get help.

If you believe there is an issue in Django, then please follow our
[https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-
features/#reporting-bugs bug reporting guidelines] and include a clear,
concise description of the problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:2>

Django

unread,
May 22, 2023, 7:54:04 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

Also I just tested exclude with nested fks and it's working ok.

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:3>

Django

unread,
May 22, 2023, 9:32:31 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.1

(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* version: 4.2 => 4.1


--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:4>

Django

unread,
May 22, 2023, 9:44:32 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2

(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:

| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* keywords: => 4.1
* version: 4.1 => 4.2


--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:5>

Django

unread,
May 22, 2023, 9:44:39 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* status: closed => new
* resolution: invalid =>


--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:6>

Django

unread,
May 22, 2023, 9:59:26 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* status: new => closed
* resolution: => invalid


Comment:

Hi ftamy9

Please don't reopen the ticket unless more information is provided. You'll
need to paste your models (only the necessary bits required for testing) +
the query you tried that wasn't working.

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:7>

Django

unread,
May 22, 2023, 10:06:06 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by ftamy9:

Old description:

New description:

There is a problem on exclude. you can not use nested ForeignKey. I tested
on 4.2.1 and 4.1.
for example if you need to find trips that are not
`trip__method__title=ShippingChoice.ORGANIZATION`
Here is the code:

{{{


working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
],
trip__method__title=ShippingChoice.ORGANIZATION
).filter(
updated_at__lt=time_threshold,
)
}}}

So you are forced to use a big list on filter instead to use the exclude.
like this:

{{{
working_shipments = Shipment.objects.exclude(
state__in=[
ShipmentStateChoices.CANCELLED,
ShipmentStateChoices.DELIVERED
]
).filter(
updated_at__lt=time_threshold,
trip__method__title__in=(ShippingChoice.ALO, ShippingChoice.SNAP,
ShippingChoice.MIAREH)
)

}}}
the objects.exclude bug need to fix

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:8>

Django

unread,
May 22, 2023, 10:06:23 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by ftamy9:

Old description:

> There is a problem on exclude. you can not use nested ForeignKey. I

New description:

}}}
the objects.exclude bug need to fixed

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:9>

Django

unread,
May 22, 2023, 10:21:38 AM5/22/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

There are a few known limitations / bugs with `exclude` (#14645, #27867,
#25991) that could be the cause here but it's impossible to tell without
the exact model definition and test setup to reproduce.

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:10>

Django

unread,
May 23, 2023, 5:00:31 AM5/23/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* Attachment "trip.py" added.

Django

unread,
May 23, 2023, 5:01:17 AM5/23/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* Attachment "shipment.py" added.

Django

unread,
May 23, 2023, 5:02:00 AM5/23/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* Attachment "method.py" added.

Django

unread,
May 23, 2023, 5:04:25 AM5/23/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ftamy9):

* status: closed => new
* resolution: invalid =>


Comment:

the model files attached

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:11>

Django

unread,
May 23, 2023, 5:20:18 AM5/23/23
to django-...@googlegroups.com
#34589: exclude does not support nested ForeignKey relationship
-------------------------------------+-------------------------------------
Reporter: ftamy9 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: 4.1 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

The following query works for me:
{{{
working_shipments = Shipment.objects.exclude(
trip__method__title=ShippingChoice.ORGANIZATION
).filter(
updated_at__lt=datetime.datetime.now(),
)
}}}
Again, please try to use support channels.

--
Ticket URL: <https://code.djangoproject.com/ticket/34589#comment:12>

Reply all
Reply to author
Forward
0 new messages