Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Add array of array of values type. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions dbarray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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)


TextArrayArrayField = array_array_field_factory('TextArrayArrayField', models.TextField)