Skip to content

Instantly share code, notes, and snippets.

@exit99
Created November 17, 2014 18:11
Show Gist options
  • Save exit99/fa312bd9ef1ba812af5a to your computer and use it in GitHub Desktop.
Save exit99/fa312bd9ef1ba812af5a to your computer and use it in GitHub Desktop.
import ast
from django.db import models
class ListField(models.TextField):
__metaclass__ = models.SubfieldBase
description = "Stores a python list"
# We use this to store user pks on flags.
def __init__(self, *args, **kwargs):
super(ListField, self).__init__(*args, **kwargs)
def to_python(self, value):
if not value:
value = []
if isinstance(value, list):
return value
return ast.literal_eval(value)
def get_prep_value(self, value):
if value is None:
return value
return unicode(value)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_db_prep_value(value)
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^intranet\.plans\.fields\.ListField"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment