[Django] #24592: order_by HStore Key/Value Pair

47 views
Skip to first unread message

Django

unread,
Apr 6, 2015, 9:46:02 PM4/6/15
to django-...@googlegroups.com
#24592: order_by HStore Key/Value Pair
------------------------------+--------------------------------------------
Reporter: DavidMuller | Owner:
Type: | Status: new
Uncategorized |
Component: | Version: 1.8
contrib.postgres |
Severity: Normal | Keywords: hstore postgres order_by value
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------------
I am currently trying out Django 1.8's HStore implementation, but am
having trouble using the ORM's order_by function.

Say I have HStore dictionaries stored in my database like

{{{
{'key': 'value', 'en-US': 'english'}
}}}

and would like to order a queryset by the strings pointed to by one
particular key.

I run into 2 distinct errors trying to 'order_by' either key ('key' or
'en-US'):

1.
{{{
MyModel.objects.order_by('name__key')

/usr/local/etc/virtualenvs/gears/local/lib/python2.7/site-
packages/django/db/models/sql/query.py in names_to_path(self, names, opts,
allow_many, fail_on_missing)
1427 raise FieldError(
1428 "Cannot resolve keyword %r into field.
Join on '%s'"
-> 1429 " not permitted." % (names[pos + 1],
name))
1430 break
1431 return path, final_field, targets, names[pos + 1:]

FieldError: Cannot resolve keyword 'key' into field. Join on 'name' not
permitted.
}}}


2.
{{{
MyModel.objects.order_by('name__en-US')

/usr/local/etc/virtualenvs/gears/local/lib/python2.7/site-
packages/django/db/models/sql/query.py in add_ordering(self, *ordering)
1717 errors.append(item)
1718 if errors:
-> 1719 raise FieldError('Invalid order_by arguments: %s' %
errors)
1720 if ordering:
1721 self.order_by.extend(ordering)

FieldError: Invalid order_by arguments: ['name__en-US']
}}}

Is ordering by a particular key/value pair not something the ORM supports
(or plans on supporting)?

--
Ticket URL: <https://code.djangoproject.com/ticket/24592>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 6, 2015, 11:40:36 PM4/6/15
to django-...@googlegroups.com
#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

unread,
Apr 9, 2015, 1:28:21 AM4/9/15
to django-...@googlegroups.com
#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
-------------------------------------+-------------------------------------

Comment (by DavidMuller):

Thanks for the very helpful reply @jarshaw. Looking forward to seeing
support in a later Django release

--
Ticket URL: <https://code.djangoproject.com/ticket/24592#comment:2>

Django

unread,
Apr 30, 2015, 10:17:49 AM4/30/15
to django-...@googlegroups.com
#24592: order_by HStore Key/Value Pair
-------------------------------------+-------------------------------------
Reporter: DavidMuller | Owner:
Type: New feature | Status: new
Component: contrib.postgres | Version: master

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 timgraham):

* version: 1.8 => master
* type: Uncategorized => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/24592#comment:3>

Django

unread,
May 1, 2015, 7:25:01 AM5/1/15
to django-...@googlegroups.com
#24592: order_by HStore Key/Value Pair
-------------------------------------+-------------------------------------
Reporter: DavidMuller | Owner:
Type: New feature | Status: new
Component: contrib.postgres | Version: master
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
-------------------------------------+-------------------------------------

Comment (by mjtamlyn):

This is heavily related to #23709

--
Ticket URL: <https://code.djangoproject.com/ticket/24592#comment:4>

Django

unread,
May 4, 2015, 7:28:08 PM5/4/15
to django-...@googlegroups.com
#24592: order_by HStore Key/Value Pair
-------------------------------------+-------------------------------------
Reporter: DavidMuller | Owner:
Type: New feature | Status: closed
Component: contrib.postgres | Version: master
Severity: Normal | Resolution: duplicate

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):

* status: new => closed
* resolution: => duplicate


Comment:

Closing as a duplicate of https://code.djangoproject.com/ticket/24747

--
Ticket URL: <https://code.djangoproject.com/ticket/24592#comment:5>

Reply all
Reply to author
Forward
0 new messages