#34589 closed Bug (invalid)

exclude does not support nested ForeignKey relationship

Reported by: ftamy9 Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: 4.1
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ftamy9)

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 fixed

Attachments (3)

trip.py (1.7 KB ) - added by ftamy9 12 months ago.
shipment.py (7.1 KB ) - added by ftamy9 12 months ago.
method.py (2.4 KB ) - added by ftamy9 12 months ago.

Download all attachments as: .zip

Change History (15)

comment:1 by David Sanders, 12 months ago

Description: modified (diff)

formatted description

comment:2 by Mariusz Felisiak, 12 months ago

Resolution: invalid
Status: newclosed

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 bug reporting guidelines and include a clear, concise description of the problem.

comment:3 by David Sanders, 12 months ago

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

comment:4 by ftamy9, 12 months ago

Version: 4.24.1

comment:5 by ftamy9, 12 months ago

Keywords: 4.1 added
Version: 4.14.2

comment:6 by ftamy9, 12 months ago

Resolution: invalid
Status: closednew

comment:7 by David Sanders, 12 months ago

Resolution: invalid
Status: newclosed

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.

comment:8 by ftamy9, 12 months ago

Description: modified (diff)

comment:9 by ftamy9, 12 months ago

Description: modified (diff)

comment:10 by Simon Charette, 12 months ago

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.

by ftamy9, 12 months ago

Attachment: trip.py added

by ftamy9, 12 months ago

Attachment: shipment.py added

by ftamy9, 12 months ago

Attachment: method.py added

comment:11 by ftamy9, 12 months ago

Resolution: invalid
Status: closednew

the model files attached

comment:12 by Mariusz Felisiak, 12 months ago

Resolution: invalid
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top