Opened 10 years ago

Closed 9 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 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Josh Smeaton, 9 years ago

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 by Josh Smeaton, 9 years ago

The PR attached may also fix #22288.

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

comment:5 by Josh Smeaton, 9 years ago

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

comment:6 by Tim Graham, 9 years ago

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

comment:7 by Tim Graham, 9 years ago

Patch needs improvement: unset

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

comment:8 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: newclosed

In 534aaf5:

Fixed #24629 -- Unified Transform and Expression APIs

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