Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from natgeosociety/dj-1.11
Browse files Browse the repository at this point in the history
Dj 1.11
  • Loading branch information
rcristian committed Jan 15, 2019
2 parents 0bac1e1 + fa0a236 commit 6f8d27a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ build*
*.egg*

doc/index.html
.idea

26 changes: 25 additions & 1 deletion djorm_pgarray/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,27 @@ def _unserialize(value):
return _cast_to_unicode(value)


class ArrayField(six.with_metaclass(models.SubfieldBase, models.Field)):

class Creator(object):
"""
Field descriptor that calls the to_python method on assignment.
This matches the Django<=1.9 fields.subclassing.Creator class.
See the Django 1.8 release notes where SubFieldBase was deprecated for
more: https://docs.djangoproject.com/en/1.10/releases/1.8/#subfieldbase
"""
def __init__(self, field):
self.field = field

def __get__(self, obj, type=None):
if obj is None:
return self
return obj.__dict__[self.field.name]

def __set__(self, obj, value):
obj.__dict__[self.field.name] = self.field.to_python(value)


class ArrayField(models.Field):
empty_strings_allowed = False

def __init__(self, dbtype="int", type_cast=None, dimension=1, *args, **kwargs):
Expand All @@ -120,6 +140,10 @@ def __init__(self, dbtype="int", type_cast=None, dimension=1, *args, **kwargs):
kwargs.setdefault("default", None)
super(ArrayField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, **kwargs):
super(ArrayField, self).contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, Creator(self))

def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
if lookup_type == "contains":
return [self.get_prep_value(value)]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name = "djorm-ngs-pgarray",
version = "1.2.4",
version = "1.3.0",
url = "https://github.com/niwibe/djorm-pgarray",
license = "BSD",
platforms = ["OS Independent"],
Expand Down

0 comments on commit 6f8d27a

Please sign in to comment.