Opened 8 years ago

Closed 7 years ago

#24629 closed Cleanup/optimization (fixed)

Allow Func Expressions to be used as Transforms

Reported by: Josh Smeaton Owned by: Josh Smeaton
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: 1.9
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I can't seem to find a ticket mentioning this, though we have discussed it before. Please close if you can find a related ticket.

Func Expressions and Transforms are extremely similar in API, but not enough to be interchangeable. Transforms are 1-arity (usually - postgres.array.IndexTransform is a notable exception) Functions with special behaviour.

Instead of implementing a Length Func and a Length Transform, we should be able to implement a Length Func and register that same class as a Transform. That's not currently possible because of the slight differences in API and behaviour. We should resolve this ASAP.

Change History (9)

comment:2 Changed 8 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:3 Changed 7 years ago by Josh Smeaton

Has patch: set
Needs documentation: set
Needs tests: set
Patch needs improvement: set

https://github.com/django/django/pull/5090 is the newest PR to tackle this patch. It still needs some tidying up, docs, and maybe some tests. But I'm happier with the approach used here than the previous couple we've thrown together.

There's no bridge/compat layer here. Instead, Transforms are now handled independently from Lookups, allowing Transform to be removed and replaced with Func. A nice side effect of converting Transforms to expressions is that the RHS of lookups can now be an expression, paving the way for an object based filtering pattern similar to:

Model.objects.filter(StartsWith(F('field'), Lower(Value('my_value')))

This trivial example isn't as nice as the traditional string based filtering, but it's easy to imagine situations where you're passing around objects that can be used as annotations or filters, and just dumping them straight in unaltered.

comment:4 Changed 7 years ago by Josh Smeaton

The PR attached may also fix #22288.

Last edited 7 years ago by Tim Graham (previous) (diff)

comment:5 Changed 7 years ago by Josh Smeaton

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 Changed 7 years ago by Tim Graham

Keywords: 1.9 added
Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:7 Changed 7 years ago by Tim Graham

Patch needs improvement: unset

Seems to be ready for more review as tests are now passing.

comment:8 Changed 7 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:9 Changed 7 years ago by Josh Smeaton <josh.smeaton@…>

Resolution: fixed
Status: newclosed

In 534aaf5:

Fixed #24629 -- Unified Transform and Expression APIs

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