This django.db.models.Field stores a list of python strings in one database column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # -*- coding: iso-8859-1 -*-
# $Id: StringListField.py 344 2009-05-06 06:57:27Z tguettler $
# $HeadURL: svn+ssh://svnserver/svn/djangotools/trunk/dbfields/StringListField.py $
# http://www.djangosnippets.org/snippets/1491/
# Django
from django.db import models
from django import forms
class StringListField(models.Field):
u'''
Save a list of strings in a CharField (or TextField) column.
In the django model object the column is a list of strings.
'''
__metaclass__=models.SubfieldBase
SPLIT_CHAR=u'\v'
def __init__(self, *args, **kwargs):
self.internal_type=kwargs.pop('internal_type', 'CharField') # or TextField
super(StringListField, self).__init__(*args, **kwargs)
def to_python(self, value):
if isinstance(value, list):
return value
if value is None:
return []
return value.split(self.SPLIT_CHAR)
def get_internal_type(self):
return self.internal_type
def get_db_prep_lookup(self, lookup_type, value):
# SQL WHERE
raise NotImplementedError()
def get_db_prep_save(self, value):
return self.SPLIT_CHAR.join(value)
def formfield(self, **kwargs):
assert not kwargs, kwargs
return forms.MultipleChoiceField(choices=self.choices)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
I've modified this locally to handle saving and loading empty values better.
In get_db_prep_save (line 37-38), I added:
without this, an empty string was saved -- I prefer a NULL in the db.
Line 26-27, I changed this to
#
When using choices, in order to be able to use the widget, I had to add a validate method:
#
Please login first before commenting.