2013-08-08 14:02:12 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
2013-08-11 15:32:45 +00:00
|
|
|
try:
|
|
|
|
|
unicode()
|
|
|
|
|
str_class = basestring
|
|
|
|
|
except NameError:
|
|
|
|
|
str_class = str
|
|
|
|
|
|
|
|
|
|
def with_metaclass(meta, base=object):
|
|
|
|
|
return meta("NewBase", (base,), {})
|
2013-08-08 14:02:12 +00:00
|
|
|
|
|
|
|
|
|
2013-08-11 15:32:45 +00:00
|
|
|
class MutableField(with_metaclass(models.SubfieldBase, models.TextField)):
|
2013-08-08 14:02:12 +00:00
|
|
|
|
|
|
|
|
def to_python(self, value):
|
2013-08-11 15:32:45 +00:00
|
|
|
if value == '':
|
2013-08-08 14:02:12 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
try:
|
2013-08-11 15:32:45 +00:00
|
|
|
if isinstance(value, str_class):
|
|
|
|
|
return [int(i) for i in value.split(',')]
|
2013-08-08 14:02:12 +00:00
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def get_db_prep_save(self, value, connection):
|
2013-08-11 15:32:45 +00:00
|
|
|
if value is None:
|
|
|
|
|
return ''
|
2013-08-08 14:02:12 +00:00
|
|
|
|
2013-08-11 15:32:45 +00:00
|
|
|
if isinstance(value, list):
|
|
|
|
|
value = ','.join((str(i) for i in value))
|
2013-08-08 14:02:12 +00:00
|
|
|
|
2013-08-11 15:32:45 +00:00
|
|
|
return super(MutableField, self).get_db_prep_save(value, connection)
|