#24592: order_by HStore Key/Value Pair
-------------------------------------+-------------------------------------
Reporter: DavidMuller | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.8
Severity: Normal | Resolution:
Keywords: hstore postgres | Triage Stage: Accepted
order_by value |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
This is definitely something we'd like to support, but I'm not sure if
we're there just yet. To elaborate, I don't think we have support for
Transforms in either `order_by` or `values`. To a user that might not mean
much (since we're conceptually just spanning relationships using the
double underscore `__` notation), but I'm not sure the implementation was
very aware of transforms when we updated order_by.
There are possibly two ways to fix this situation.
1. Make all Transforms into Expressions. That is, we need to consolidate
the very close APIs of Transforms/Lookups and Expressions. We have and we
are discussing this elsewhere (though I can't find where just at the
moment). This is the most likely solution we'll converge on.
2. Make .values and .order_by understand the transformation/lookup API.
We'll probably steer clear of this as it helps to widen the gap between
transforms/expressions.
You can work around this in user code by implementing an `Expression` that
emulates the `KeyTransform` transformation. Something like:
{{{
# UNTESTED!
class KeyAccessor(Func):
function = ''
arg_joiner = ' -> '
output_field = TextField()
def __init__(self, lookup, **extra):
key = lookup.split('__')[-1]
field = lookup.rstrip('__' + key)
super(KeyAccessor, self).__init__(F(field), Value(key), **extra)
Model.objects.order_by(KeyAccessor('related__hstore__keyname').desc())
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24592#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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/069.2300c37e15aeb0d5a103617d9c16ed9e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.