Skip to content

Commit

Permalink
Merge pull request #115 from ambitioninc/master
Browse files Browse the repository at this point in the history
Merge master -> develop
  • Loading branch information
geophphrie authored Dec 1, 2022
2 parents 79f1998 + 0632531 commit 2ed7b41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Release Notes
=============

v3.0.2
------
* Add `json_cursor` to handle django no longer automatically parsing json fields

v3.0.1
------
* Switch to github actions
Expand Down
16 changes: 16 additions & 0 deletions querybuilder/cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import contextlib
import json
import psycopg2


@contextlib.contextmanager
def json_cursor(django_database_connection):
"""
Cast json fields into their specific types to account for django bugs
https://code.djangoproject.com/ticket/31956
https://code.djangoproject.com/ticket/31973
https://www.psycopg.org/docs/extras.html#psycopg2.extras.register_default_jsonb
"""
with django_database_connection.cursor() as cursor:
psycopg2.extras.register_default_jsonb(conn_or_curs=cursor.cursor, loads=json.loads)
yield cursor
15 changes: 15 additions & 0 deletions querybuilder/tests/cursor_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import connection

from querybuilder.cursor import json_cursor
from querybuilder.tests.base import QuerybuilderTestCase
from querybuilder.tests.models import MetricRecord


class JsonCursorTests(QuerybuilderTestCase):
def test_json_cursor(self):
data = {'one': 1, 'two': 2}
MetricRecord.objects.create(data=data)
with json_cursor(connection) as cursor:
cursor.execute(f'select data from {MetricRecord._meta.db_table}')
record = cursor.fetchone()
self.assertEqual(record[0], data)
2 changes: 1 addition & 1 deletion querybuilder/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.0.1'
__version__ = '3.0.2'

0 comments on commit 2ed7b41

Please sign in to comment.