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
|
||||
__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):
|
||||
"""Convert our string value to python after we load it from the DB"""
|
||||
if value == None:
|
||||
# django 1.7
|
||||
if value is None:
|
||||
return value
|
||||
if isinstance(value, dict):
|
||||
if isinstance(value, self._type):
|
||||
return value
|
||||
try:
|
||||
value = json.loads(value, object_hook=from_json)
|
||||
except: #this is required to load fixtures
|
||||
value = eval(value)
|
||||
assert isinstance(value, dict)
|
||||
value = self.loads(value)
|
||||
except:
|
||||
raise Exception('failed to parse value: %s' % value)
|
||||
if value is not None:
|
||||
assert isinstance(value, self._type)
|
||||
return value
|
||||
|
||||
def get_db_prep_save(self, value, connection):
|
||||
"""Convert our JSON object to a string before we save"""
|
||||
if value == None:
|
||||
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)
|
||||
def from_db_value(self, value, expression, connection, context):
|
||||
# django 1.8
|
||||
return self.to_python(value)
|
||||
|
||||
class TupleField(models.TextField):
|
||||
"""TupleField is a textfield that contains JSON-serialized tuples."""
|
||||
|
||||
# Used so to_python() is called
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
def to_python(self, value):
|
||||
"""Convert our string value to JSON after we load it from the DB"""
|
||||
if isinstance(value, tuple):
|
||||
def get_prep_value(self, value):
|
||||
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
|
||||
|
||||
try:
|
||||
value = json.loads(value, object_hook=from_json)
|
||||
except: #this is required to load fixtures
|
||||
value = eval(value)
|
||||
assert isinstance(value, list)
|
||||
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)
|
||||
class TupleField(DictField):
|
||||
_type = tuple
|
||||
|
||||
def loads(self, value):
|
||||
value = DictField.loads(self, 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)
|
||||
return value
|
||||
|
||||
try:
|
||||
from south.modelsinspector import add_introspection_rules
|
||||
|
|
Loading…
Reference in a new issue