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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 | #-*- coding: utf-8 -*-
from django import forms
from django.core import exceptions
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.utils import simplejson
from django.utils.translation import ugettext_lazy as _
class DictionaryField(models.Field):
description = _("Dictionary object")
__metaclass__ = models.SubfieldBase
def get_internal_type(self):
return "TextField"
def to_python(self, value):
if value is None:
return None
elif value == "":
return {}
elif isinstance(value, basestring):
try:
return dict(simplejson.loads(value))
except (ValueError, TypeError):
raise exceptions.ValidationError(self.error_messages['invalid'])
if isinstance(value, dict):
return value
else:
return {}
def get_prep_value(self, value):
if not value:
return ""
elif isinstance(value, basestring):
return value
else:
return simplejson.dumps(value)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
def clean(self, value, model_instance):
value = super(DictionaryField, self).clean(value, model_instance)
return self.get_prep_value(value)
def formfield(self, **kwargs):
defaults = {'widget': forms.Textarea}
defaults.update(kwargs)
return super(DictionaryField, self).formfield(**defaults)
# rules for South migrations tool (for version >= 0.7)
try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^carting\.fields\.DictionaryField"])
except ImportError:
pass
|
Comments
What's up with all the custom model field snippets swallowing exceptions and failing silently in to_python() ?!? In addition this fails by: (1) missing the
__metaclass__ = models.SubfieldBaseand (2) using an inexistent (as of 1.1 at least) models.Field.clean() method.#
gsakkis, thank you for feedback.
snippet was written for and tested with Django 1.2, and that was pointed in description
I've added
__metaclass__definition and raising validation error into_python()Actually, I'm trying not using this approach anymore. That worked for me, but than accured that storing key=>value data in field like that is not convinient. I beleive there are cases when it's useful, but it seems they are rare.
#
Great Field ! Very usefull !
#