From 4c20457e4d8b0cd0715b54b483e522ca15a5417f Mon Sep 17 00:00:00 2001 From: Maxim Gurets Date: Thu, 29 Dec 2011 09:53:27 +0100 Subject: [PATCH] Add array of array of values type. --- dbarray/__init__.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/dbarray/__init__.py b/dbarray/__init__.py index 4c168cd..67174a3 100644 --- a/dbarray/__init__.py +++ b/dbarray/__init__.py @@ -44,7 +44,22 @@ def run_validators(self, value): else: for v in value: super(ArrayFieldBase, self).run_validators(v) - + + +class ArrayArrayFieldBase(ArrayFieldBase): + """Django field type for an array of array of values. Supported only on PostgreSQL. + + This class is not meant to be instantiated directly; instead, field classes + should inherit from this class and from an appropriate Django model class. + """ + + _south_introspects = True + + def db_type(self, connection): + require_postgres(connection) + return super(ArrayFieldBase, self).db_type(connection=connection) + '[][]' + + class ArrayFieldMetaclass(models.SubfieldBase): pass @@ -53,6 +68,13 @@ def array_field_factory(name, fieldtype, module=ArrayFieldBase.__module__): {'__module__': module, 'description': "An array, where each element is of the same type "\ "as %s." % fieldtype.__name__}) + + +def array_array_field_factory(name, fieldtype, module=ArrayArrayFieldBase.__module__): + return ArrayFieldMetaclass(name, (ArrayArrayFieldBase, fieldtype), + {'__module__': module, + 'description': "An array, where each element is of the same type "\ + "as %s." % fieldtype.__name__}) # If you want to make an array version of a field not covered below, this is # the easiest way: @@ -64,5 +86,4 @@ def array_field_factory(name, fieldtype, module=ArrayFieldBase.__module__): FloatArrayField = array_field_factory('FloatArrayField', models.FloatField) CharArrayField = array_field_factory('CharArrayField', models.CharField) TextArrayField = array_field_factory('TextArrayField', models.TextField) - - \ No newline at end of file +TextArrayArrayField = array_array_field_factory('TextArrayArrayField', models.TextField)