djangonauts / django-hstore

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

Contains lookup doesn't seem to support LIKE 'value%' SQL [was ORM Equivalent of SQL] #75

Open DavidMuller opened 10 years ago

DavidMuller commented 10 years ago

Hello, I'm having trouble forming a django ORM query for a query. In plain english, the query sounds like: "select a row where data[my_key] contains the string I'm looking for."

Concretely, consider the following model:

from django.db import models
from django_hstore import hstore

class TestHstore(models.Model):
    name = models.CharField(max_length=250)
    data = hstore.DictionaryField(db_index=True)
    objects = hstore.HStoreManager()

    def __unicode__(self):
        return self.name

The following model instance:

instance = TestHstore.objects.create(name="test", data={"en_us": "english", "french": "fries"})

I'd like to make use the Django ORM that makes a query equivalent to the following SQL:

SELECT * 
FROM hstore_test_testhstore 
WHERE data->'french' LIKE 'fr%';

I can't seem to find a way to do that within the ORM. TestHstore.objects.filter(data__contains={'en_us': "english"}) works for exact matches of the value in the key/value pair, but fails to return a database row when the value doesn't match completely (TestHstore.objects.filter(data__contains={'en_us': "eng"}))

Any Suggestions?

Thanks for this cool package--really great work!

nemesifier commented 10 years ago

Hi,

good point.

I changed the title of the issue and forward this to the mailing list. https://groups.google.com/forum/#!forum/django-hstore

DavidMuller commented 10 years ago

Thanks for doing that Federico!

A little more succinctly what I'd like to do is: do a filter operation on the value of a specific key.

The following query is incorrect syntactically, but conveys the type of query I'd like to make: SomeHstoreModel.objects.filter(hstore_attribute['specific_key']__contains="string I am looking for")

I'll add the above to the mailing list thread.

nemesifier commented 9 years ago

Sorry for the delay in the answer, we had many issue to resolve. At the moment this is not possible so I marked this as a feature request.