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

Deserialization issue #14

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions djorm_hstore/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from . import forms, util
import sys
import json

if sys.version_info[0] < 3:
text_type = unicode
Expand Down Expand Up @@ -81,8 +82,15 @@ def get_prep_lookup(self, lookup, value):
return value

def to_python(self, value):
if isinstance(value, basestring):
return json.loads(value)
return value or {}

def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
prepped = self.get_prep_value(value)
return json.dumps(prepped)

def _value_to_python(self, value):
return value

Expand Down
7 changes: 7 additions & 0 deletions djorm_hstore/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import connections
from django.db.models.aggregates import Count
from django.utils.unittest import TestCase
from django.core import serializers

from ..functions import HstoreKeys, HstoreSlice, HstorePeek
from ..expressions import HstoreExpression
Expand Down Expand Up @@ -275,6 +276,12 @@ def test_empty_querying(self):
self.assertTrue(DataBag.objects.filter(data={}))
self.assertTrue(DataBag.objects.where(HstoreExpression("data").contains({})))

def test_serialize_deserialize(self):
bag = DataBag(name='bag', data={"a": "1", "b": "2"})
s = serializers.serialize('json', [bag])
for b in serializers.deserialize('json', s):
self.assertEqual(b.object.data, {"a": "1", "b": "2"})


class TestReferencesField(TestCase):
def setUp(self):
Expand Down