Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#34589 closed Bug (invalid)

exclude does not support nested ForeignKey relationship — at Version 8

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 fix

Change History (8)

comment:1 by David Sanders, 18 months ago

Description: modified (diff)

formatted description

comment:2 by Mariusz Felisiak, 18 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, 18 months ago

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

comment:4 by ftamy9, 18 months ago

Version: 4.24.1

comment:5 by ftamy9, 18 months ago

Keywords: 4.1 added
Version: 4.14.2

comment:6 by ftamy9, 18 months ago

Resolution: invalid
Status: closednew

comment:7 by David Sanders, 18 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, 18 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top