djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

How to use with order_by or ordering? #31

Closed aidanlister closed 10 years ago

aidanlister commented 10 years ago

I'd expect to be able to do something like: Model.objects.all().order_by('data->door_order_no')

Could this be implemented somehow?

nemesifier commented 10 years ago

I guess everything is possibble if anyone works on it.

@niwibe do you think it's possible and if yes do you have a rough idea of how to accomplish it?

niwinz commented 10 years ago

Hi!

As far as I know, postgresql has support for ordering over some expresion like that but, at this momment I don't know how is difficult of implement it over django orm. I need some time for research about it.

aidanlister commented 10 years ago

Yeah, postgresql supports it, I can't trick the ORM into using it though.

I've tried all sorts of tricks: ReportItem.objects.all().extra(select={'foo': 'extra->order_no'}, order_by=['foo'])

No deal.

mjtamlyn commented 10 years ago

With the reimplemented version of hstore hopefully coming in Django 1.8, and an expansion of custom lookups to allow use elsewhere that in filter() and exclude(), this this should become possible. So hopefully Django 1.8 or 1.9. However at present I can't think of a way to achieve it without raw SQL.

I would point out that without the relevant index this will be abominably slow.

nemesifier commented 10 years ago

@mjtamlyn thanks for the info.

it was also discussed before how to add indexes on HSTORE but we concluded that it is only possible to do it manually, what do you think?

mjtamlyn commented 10 years ago

At present yes, by hand is the only option (except db_index=True). You can of course put the SQL in a migration which would be better.

nemesifier commented 10 years ago

I'm closing this as apparently it can't be solved right now.