#24833: Annotation with `Case` can't be used with `exclude` -------------------------------------+------------------------------------- Reporter: coolRR | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.8 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by coolRR):
* needs_better_patch: => 0 * needs_tests: => 0 * needs_docs: => 0 Old description: > n 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.) New description: 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.) -- -- Ticket URL: <https://code.djangoproject.com/ticket/24833#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.a5890d9504bd9acf9e47e643f64f5aa7%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.