Opened 10 years ago

Closed 9 years ago

Last modified 9 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 10 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 10 years ago.

Download all attachments as: .zip

Change History (12)

by Ram Rachum, 10 years ago

Attachment: sample.zip added

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

comment:1 by Ram Rachum, 10 years ago

Description: modified (diff)

comment:2 by Tim Graham, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

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

by Tim Graham, 10 years ago

Attachment: 24833-test.diff added

comment:3 by Simon Charette, 10 years ago

Triage Stage: AcceptedReady for checkin

Added regression test LGTM.

comment:4 by Tim Graham, 10 years ago

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 by Ram Rachum, 9 years ago

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

comment:6 by Andriy Sokolovskiy, 9 years ago

Owner: changed from nobody to Andriy Sokolovskiy
Status: newassigned

Talked with Anssi, going to fix that

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

Resolution: fixed
Status: assignedclosed

In 469f1e3:

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

comment:10 by Tim Graham <timograham@…>, 9 years ago

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