Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#24833 closed Bug (fixed)

Annotation with `Case` can't be used with `exclude`

Reported by: Ram Rachum Owned by: Andriy Sokolovskiy
Component: Database layer (models, ORM) Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ram Rachum)

When you define an annotation that uses Case, you can use .filter on it but you can't use .exclude. (You get various exceptions about .null and .get_col not existing.)

Example:

def my_view(request, *args, **kwargs):
    things = models.Thing.objects.all().annotate(
        bar=django.db.models.Case(
            django.db.models.When(
                foo='foo',
                then=django.db.models.Value('bar')
            ),
            default=django.db.models.Value('baz'),
            output_field=django.db.models.CharField()
        )
    )
    
    print(things.filter(bar='bar')) # This one works
    print(things.exclude(bar='bar')) # This one doesn't 
    return django.http.HttpResponse(str(things))

Full working example attached to this ticket. (Simply run migrate, then runserver, then access the root URL to see the Django debug page for the exception.)

Attachments (2)

sample.zip (15.5 KB) - added by Ram Rachum 5 years ago.
Full working example, run migrate then runserver then access root url to see the exception
24833-test.diff (933 bytes) - added by Tim Graham 5 years ago.

Download all attachments as: .zip

Change History (12)

Changed 5 years ago by Ram Rachum

Attachment: sample.zip added

Full working example, run migrate then runserver then access root url to see the exception

comment:1 Changed 5 years ago by Ram Rachum

Description: modified (diff)

comment:2 Changed 5 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Adding a regression test for 1.8. It seems this was fixed in master by b68212f539f206679580afbfd008e7d329c9cd31.

Changed 5 years ago by Tim Graham

Attachment: 24833-test.diff added

comment:3 Changed 5 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

Added regression test LGTM.

comment:4 Changed 5 years ago by Tim Graham

Triage Stage: Ready for checkinAccepted

I think we should try to fix the problem on 1.8 since it's a crashing bug in a new feature.

comment:5 Changed 5 years ago by Ram Rachum

I agree with Tim, for what it's worth.

comment:6 Changed 5 years ago by Andriy Sokolovskiy

Owner: changed from nobody to Andriy Sokolovskiy
Status: newassigned

Talked with Anssi, going to fix that

comment:7 Changed 5 years ago by Andriy Sokolovskiy

Has patch: set

comment:8 Changed 5 years ago by Andriy Sokolovskiy

comment:9 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 469f1e3:

[1.8.x] Fixed #24833 -- Fixed Case expressions with exclude().

comment:10 Changed 5 years ago by Tim Graham <timograham@…>

In 286d0e6a:

Refs #24833 -- Forwardported some of "Fixed Case expressions with exclude()."

Partial forwardport of 469f1e362bb9670b174b37da9edd4631aff7badb from stable/1.8.x
as the issue was already fixed in master.

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