Login

Custom model field to store dict object in database

Author:
rudyryk
Posted:
April 7, 2010
Language:
Python
Version:
1.1
Score:
1 (after 3 ratings)

Disclaimer

This is proof of concept snippet, It can not be considered as best practice. Seems like it's better to store (key => value) in sepetare model with 'key' and 'value' fields.

Example

You can assign any dict to model field that can be dumped/serialized with Django json encoder/decoder. Data is saved as TextField in database, empty dict is saved as empty text. Tested with Django 1.2beta.

import DictionaryField

class UserData(models.Model):
    user = models.ForeignKey(User)
    meta = DictionaryField(blank=True)

There are similar snippets: JSONField 1 or JSONField 2.

 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

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

gsakkis (on April 15, 2010):

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.SubfieldBase and (2) using an inexistent (as of 1.1 at least) models.Field.clean() method.

#

rudyryk (on June 4, 2010):

gsakkis, thank you for feedback.

  1. snippet was written for and tested with Django 1.2, and that was pointed in description

  2. I've added __metaclass__ definition and raising validation error in to_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.

#

sberlotto (on August 6, 2010):

Great Field ! Very usefull !

#

Please login first before commenting.