Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DavidMuller opened this issue Oct 14, 2014 · 3 comments

Comments

@DavidMuller
Copy link

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 nemesifier changed the title ORM Equivalent of SQL Contains lookup doesn't seem to support LIKE 'value%' SQL [was ORM Equivalent of SQL] Oct 15, 2014
@nemesifier
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants