Search by entering to value in JSONField PostgreSQL

23 views
Skip to first unread message

Алексей Кузуб

unread,
Mar 23, 2018, 4:20:57 PM3/23/18
to Django users

Hi! I work with poorly structured data. I use EAV. But I want to replace EAV with jsonb in postgresql. I need to do a search by entering to value in json.values(). Is it possible? Is there some decision on Django or SQL?For example, there is the model

class SomeModel(models.Model):
    data = JSONField()

That jsons stored in rows in data field:

{"name": "Michael", "friend_name": "Sara"}
{"name": "Miranda", "friend_name": "Richard"}

If I type "ar" in search input I want to get both rows("ar" in "Sara""ar" in "Miranda"), "cha" - both rows( "cha" in "Michael""ar" in "Richard"), "ir" - only one ("ir" in "Miranda"), "asdq" - nothing. Is it possible with Django? Is it possible with SQL? Thank you and sorry for my English.

Michael MacIntosh

unread,
Mar 23, 2018, 5:33:32 PM3/23/18
to django...@googlegroups.com

Hi,

Did you try

SomeModel.objects.filter(data__name__contains="ar")

If you want to union the results from both fields you can use a Q object:

https://docs.djangoproject.com/en/2.0/topics/db/queries/#complex-lookups-with-q-objects

And that would look something like:

complex_query = Q(data__name__contains="ar") | Q(data__friend_name__contains="ar)
SomeModel.objects.filter( complex_query )

Hope that helps.

Cheers!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a3bb18e9-c513-493b-a3a8-5f8da1a40b81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.

Алексей Кузуб

unread,
Mar 24, 2018, 11:43:29 AM3/24/18
to Django users
Thank's for your answer. As I know when we are using JSONField Django translate __contains in SQL with @> that check entering json in json. So it can't help me.
Reply all
Reply to author
Forward
0 new messages