* move Dict/Tuple field code into python-oxdjango

This commit is contained in:
j 2010-01-23 10:09:07 +11:00
parent ac117505b6
commit 2d90cfaa51
2 changed files with 3 additions and 49 deletions

View file

@ -1,47 +0,0 @@
from django.db import models
from django.utils import simplejson as json
class DictField(models.TextField):
"""DictField is a textfield that contains JSON-serialized dictionaries."""
# Used so to_python() is called
__metaclass__ = models.SubfieldBase
def to_python(self, value):
"""Convert our string value to python after we load it from the DB"""
if isinstance(value, dict):
return value
value = json.loads(value)
assert isinstance(value, dict)
return value
def get_db_prep_save(self, value):
"""Convert our JSON object to a string before we save"""
assert isinstance(value, dict)
value = json.dumps(value)
return super(DictField, self).get_db_prep_save(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):
return value
value = json.loads(value)
assert isinstance(value, list)
return tuple(value)
def get_db_prep_save(self, value):
"""Convert our JSON object to a string before we save"""
assert isinstance(value, tuple)
value = json.dumps(value)
return super(TupleField, self).get_db_prep_save(value)

View file

@ -9,14 +9,15 @@ from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.utils import simplejson as json
import oxlib
from oxlib import stripTags
from oxlib.normalize import canonicalTitle, canonicalName
from django.utils import simplejson as json
from oxdjango import fields
import utils
import managers
import fields
import load
class MovieImdb(models.Model):