Opened 9 years ago

Last modified 9 years ago

#24833 closed Bug

Annotation with `Case` can't be used with `exclude` — at Version 1

Reported by: Ram Rachum Owned by: nobody
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.)

Change History (2)

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

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