Commits

James Pic  committed dff64d2

Added db_type keyword argument to JSONField

  • Participants
  • Parent commits 245e0d7
  • Branches db_type

Comments (0)

Files changed (2)

     <script>
     var foo = {{ bar|jsonify }};
     </script>
-  
+
+Override field db_type
+~~~~~~~~~~~~~~~~~~~~~~
+
+Set the db_type constructor keyword argument::
+
+    the_json = jsonfield.JSONField(db_type='json')
+
+On PostgreSQL 9.2, this will result as::
+
+         Column     |   Type   |                          Modifiers                           ----------------+----------+------------------------------------
+      the_json      | json     | not null
+ 
+Instead of default::
+
+         Column     |   Type   |                          Modifiers                           ----------------+----------+------------------------------------
+      the_default   | text     | not null
+ 
 History
 ----------
 

File jsonfield/fields.py

 from django.core.exceptions import ValidationError
+from django.conf import settings
 from django.db import models
 from django.utils import simplejson as json
 from django.utils.translation import ugettext_lazy as _
         'invalid': _(u"'%s' is not a valid JSON string.")
     }
     description = "JSON object"
-    
-    def __init__(self, *args, **kwargs):
+
+    def __init__(self, db_type=None, *args, **kwargs):
         if not kwargs.get('null', False):
             kwargs['default'] = kwargs.get('default', {})
+
+        self.db_type_override=db_type
+
         super(JSONField, self).__init__(*args, **kwargs)
         if 'default' in kwargs:
             if callable(self.default):
                 self.validate(self.default(), None)
             else:
                 self.validate(self.default, None)
-        
+
     def formfield(self, **kwargs):
         return super(JSONField, self).formfield(form_class=JSONFormField, **kwargs)
-    
+
     def validate(self, value, model_instance):
         if not self.null and value is None:
             raise ValidationError(self.error_messages['null'])
 
     def get_db_prep_value(self, value, connection=None, prepared=None):
         return self.get_prep_value(value)
-    
+
     def get_prep_value(self, value):
         if value is None:
             if not self.null and self.blank:
                 return ""
             return None
         return json.dumps(value, default=default)
-    
+
     def get_prep_lookup(self, lookup_type, value):
         if lookup_type in ["exact", "iexact"]:
             return self.to_python(self.get_prep_value(value))
     def value_to_string(self, obj):
         return self._get_val_from_obj(obj)
 
+    def db_type(self, connection):
+        if self.db_type_override:
+            return self.db_type_override
+        else:
+            return super(JSONField, self).db_type(connection)
+
 try:
     from south.modelsinspector import add_introspection_rules
     add_introspection_rules([], ['^jsonfield\.fields\.JSONField'])
 except ImportError:
-    pass
+    pass