rewrite ox.django.fields
This commit is contained in:
parent
2681536b08
commit
43783b00a1
1 changed files with 32 additions and 40 deletions
|
@ -49,57 +49,49 @@ class DictField(models.TextField):
|
||||||
|
|
||||||
# Used so to_python() is called
|
# Used so to_python() is called
|
||||||
__metaclass__ = models.SubfieldBase
|
__metaclass__ = models.SubfieldBase
|
||||||
|
_type = dict
|
||||||
|
|
||||||
|
def loads(self, value):
|
||||||
|
return json.loads(value, object_hook=from_json)
|
||||||
|
|
||||||
|
def dumps(self, obj):
|
||||||
|
return json.dumps(obj, default=to_json)
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
"""Convert our string value to python after we load it from the DB"""
|
# django 1.7
|
||||||
if value == None:
|
if value is None:
|
||||||
return value
|
return value
|
||||||
if isinstance(value, dict):
|
if isinstance(value, self._type):
|
||||||
return value
|
return value
|
||||||
try:
|
try:
|
||||||
value = json.loads(value, object_hook=from_json)
|
value = self.loads(value)
|
||||||
except: #this is required to load fixtures
|
except:
|
||||||
value = eval(value)
|
raise Exception('failed to parse value: %s' % value)
|
||||||
assert isinstance(value, dict)
|
if value is not None:
|
||||||
|
assert isinstance(value, self._type)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def get_db_prep_save(self, value, connection):
|
def from_db_value(self, value, expression, connection, context):
|
||||||
"""Convert our JSON object to a string before we save"""
|
# django 1.8
|
||||||
if value == None:
|
return self.to_python(value)
|
||||||
return value
|
|
||||||
if isinstance(value, string_types):
|
|
||||||
value = eval(value)
|
|
||||||
assert isinstance(value, dict)
|
|
||||||
value = json.dumps(value, default=to_json)
|
|
||||||
return super(DictField, self).get_db_prep_save(value, connection=connection)
|
|
||||||
|
|
||||||
class TupleField(models.TextField):
|
def get_prep_value(self, value):
|
||||||
"""TupleField is a textfield that contains JSON-serialized tuples."""
|
if isinstance(value, self._type):
|
||||||
|
value = self.dumps(value)
|
||||||
|
if value is not None:
|
||||||
|
assert isinstance(value, string_types)
|
||||||
|
value = models.TextField.get_prep_value(self, value)
|
||||||
|
return value
|
||||||
|
|
||||||
# Used so to_python() is called
|
|
||||||
__metaclass__ = models.SubfieldBase
|
|
||||||
|
|
||||||
def to_python(self, value):
|
class TupleField(DictField):
|
||||||
"""Convert our string value to JSON after we load it from the DB"""
|
_type = tuple
|
||||||
if isinstance(value, tuple):
|
|
||||||
return value
|
|
||||||
|
|
||||||
try:
|
def loads(self, value):
|
||||||
value = json.loads(value, object_hook=from_json)
|
value = DictField.loads(self, value)
|
||||||
except: #this is required to load fixtures
|
if isinstance(value, list):
|
||||||
value = eval(value)
|
value = tuple(value)
|
||||||
assert isinstance(value, list)
|
return value
|
||||||
return tuple(value)
|
|
||||||
|
|
||||||
def get_db_prep_save(self, value, connection):
|
|
||||||
"""Convert our JSON object to a string before we save"""
|
|
||||||
if isinstance(value, string_types):
|
|
||||||
value = eval(value)
|
|
||||||
if isinstance(value, list):
|
|
||||||
value = tuple(value)
|
|
||||||
assert isinstance(value, tuple)
|
|
||||||
value = json.dumps(value, default=to_json)
|
|
||||||
return super(TupleField, self).get_db_prep_save(value, connection=connection)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from south.modelsinspector import add_introspection_rules
|
from south.modelsinspector import add_introspection_rules
|
||||||
|
|
Loading…
Reference in a new issue