https://github.com/mkalioby/jsonlookup
Searching in MySQL JSON field in Django
https://github.com/mkalioby/jsonlookup
django json json-fields mysql
Last synced: 10 months ago
JSON representation
Searching in MySQL JSON field in Django
- Host: GitHub
- URL: https://github.com/mkalioby/jsonlookup
- Owner: mkalioby
- License: mit
- Created: 2016-05-04T19:50:36.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-10-27T11:21:53.000Z (over 5 years ago)
- Last Synced: 2025-06-20T04:05:41.907Z (10 months ago)
- Topics: django, json, json-fields, mysql
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonLookup
Searching in MySQL JSON field in Django
# Challenge
MySQL has introduced JSON fields in MySQL Server 5.7, Currently it works well with [jsonfield](https://github.com/bradjasper/django-jsonfield) for Data Saving and retrieval. The problem that Searching can only be done through LIKE operator which isn't convinent for all function.
# Objective
Creating a new custom lookup operator "has" for Django that will support JSON Search in MySQL.
# Installation
### Install the Package
```sh
$ pip install jsonLookup
```
### Register to JSONFields
```python
from jsonLookup import hasLookup,jcontainsLookup
JSONField.register_lookup(hasLookup)
JSONField.register_lookup(jcontainsLookup)
```
### Write your JSON queries
```python
# Create test objects
User.objects.create(name="Ahmed",properties={"city":"Giza","Address":{"district":"Ahram","Code":11263}})
User.objects.create(name="Mohamed",properties={"city":"Cairo","Address":{"district":"Helipolis","Code":11351}})
# Run first query
q = User.objects.filter(properties__has="$.city=Giza")
print q[0].name
"Ahmed"
# Run Second query
q= User.objects.filter(properties__has="$.Address.Code=11351")
print q[0].name
"Mohamed"
```