Skip to content

Commit

Permalink
Merge 37915b9 into 4930634
Browse files Browse the repository at this point in the history
  • Loading branch information
roadsideseb committed Jun 21, 2014
2 parents 4930634 + 37915b9 commit 20990b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 8 additions & 2 deletions django_pg/models/fields/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class ArrayField(models.Field):
def __init__(self, of=models.IntegerField, **kwargs):
# The `of` argument is a bit tricky once we need compatibility
# with South.
#
#
# South can't store a field, and the eval it performs doesn't
# put enough things in the context to use South's internal
# "get field" function (`BaseMigration.gf`).
#
#
# Therefore, we need to be able to accept a South triple of our
# sub-field and hook into South to get the correct thing back.
if isinstance(of, tuple) and south_installed:
Expand All @@ -46,6 +46,12 @@ def __init__(self, of=models.IntegerField, **kwargs):
# Now pass the rest of the work to the Field superclass.
super(ArrayField, self).__init__(**kwargs)

def deconstruct(self):
name, path, args, kwargs = super(ArrayField, self).deconstruct()
if self.of is not models.IntegerField:
kwargs['of'] = self.of
return name, path, args, kwargs

def create_type(self, connection):
if hasattr(self.of, 'create_type'):
return self.of.create_type(connection)
Expand Down
14 changes: 10 additions & 4 deletions django_pg/models/fields/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def __init__(self, null=True, blank=True, type=None, default=None,
super(JSONField, self).__init__(*args, null=null, blank=blank,
default=default, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super(JSONField, self).deconstruct()
if self._type is not None:
kwargs['type'] = self._type
return name, path, args, kwargs

def db_type(self, connection):
return 'json' if get_version(connection) >= 90200 else 'text'

Expand All @@ -54,7 +60,7 @@ def get_db_prep_lookup(self, lookup_type, value, connection,

def get_prep_value(self, value):
return json.dumps(value)

@validate_type
def to_python(self, value):
"""Given input that may be a Python value and may be JSON, return
Expand All @@ -63,15 +69,15 @@ def to_python(self, value):
# Lists, dicts, ints, and booleans are clearly fine as is.
if not isinstance(value, six.text_type):
return value

# Properly identify numbers and return them as ints or floats.
if self._type != six.text_type:
if re.match(r'^[\d]+$', value):
return int(value)
if (re.match(r'^[\d]+\.[\d]*$', value) or
re.match(r'^\.[\d]+$', value)):
return float(value)

# Try to tell the difference between a "normal" string
# and serialized JSON.
#
Expand All @@ -87,7 +93,7 @@ def to_python(self, value):
# this field is set to str.
if value.startswith('"') and value.endswith('"'):
return json.loads(value)

# Okay, this is not a JSON string. Return the unadulterated value.
return value

Expand Down
8 changes: 8 additions & 0 deletions django_pg/models/fields/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def __init__(self, auto_add=False, coerce_to=uuid.UUID, **kwargs):
# Now pass the rest of the work to CharField.
super(UUIDField, self).__init__(**kwargs)

def deconstruct(self):
name, path, args, kwargs = super(UUIDField, self).deconstruct()
if self._auto_add:
kwargs['auto_add'] = self._auto_add
if self._coerce_to != uuid.UUID:
kwargs['coerce_to'] = self._coerce_to
return name, path, args, kwargs

def db_type(self, connection):
return 'uuid'

Expand Down

0 comments on commit 20990b7

Please sign in to comment.