Opened 8 years ago

Closed 8 years ago

#27482 closed Cleanup/optimization (fixed)

Conditional Expressions documentation should have an example for filter() methods

Reported by: Daniel Spajic Owned by: Jonatas CD
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

There's no example on this page for using Case() inside a filter() method. There should be one, since the page clearly says "Conditional expressions let you use if ... elif ... else logic within filters, annotations, aggregations, and updates."

This is the documentation: https://docs.djangoproject.com/en/1.10/ref/models/conditional-expressions/

Change History (5)

comment:1 by Josh Smeaton, 8 years ago

Easy pickings: set
Needs documentation: set
Triage Stage: UnreviewedAccepted
Version: 1.10master

Example from IRC. Note the logic isn't correct, but the syntax works fine.

Model.objects.filter(date_from__gte=Case(
        When(Q(has_flexible_departure=True), then=house.available_from),
        When(Q(has_flexible_departure=False), then=house.available_from),
    ))

comment:2 by Jonatas CD, 8 years ago

Owner: changed from nobody to Jonatas CD
Status: newassigned

comment:3 by Jonatas CD, 8 years ago

Has patch: set

PR added.

comment:4 by Tim Graham, 8 years ago

Needs documentation: unset

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

Resolution: fixed
Status: assignedclosed

In b28c6ca7:

Fixed #27482 -- Doc'd an example of Case() in QuerySet.filter().

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