Skip to content

Instantly share code, notes, and snippets.

@fcoclavero
Last active May 2, 2018 22:11
Show Gist options
  • Save fcoclavero/c6910eb18406afaf93e509f6342a0f37 to your computer and use it in GitHub Desktop.
Save fcoclavero/c6910eb18406afaf93e509f6342a0f37 to your computer and use it in GitHub Desktop.
Custom Django model field for storing Numpy 1-d arrays as JSON strings in the database. Useful for handling small amounts of arrays, keeping the relational data scheme.
class NumpyArrayField(models.TextField):
description = "A field to store numpy 1-d arrays as json strings. Requires Pandas."
def __init__(self, *args, **kwargs):
"""
Instance the field.
:param kwargs: max_length - the maximum array length
"""
super(NumpyArrayField, self).__init__(*args, **kwargs)
def get_db_prep_value(self, value, *args, **kwargs):
"""
Converts a numpy 1-dimensional array into a json formatted string for saving in
the database as a TextField.
:param value: a numpy array being stored
:type: np.ndarray
:return: value, as a json formatted string
:type: str
"""
if value is None:
return None
return pd.Series(value).to_json(orient='values') # serialize array
def to_python(self, value):
"""
Called by deserialization and during the clean() method used from forms.
Reconstructs the numpy 1-dimensional array from the json string stored in the database.
:param value: the array as a json formatted string
:type: str
:return: value, as numpy 1-dimensional array
:type: np.ndarray
"""
if value is None or isinstance(value, np.ndarray):
return value
try:
return pd.read_json(value)[0].as_matrix() # numpy array from json string
except (TypeError, ValueError):
raise ValidationError("This value must be a json formatted string for a 1-d array.")
def from_db_value(self, value, expression, connection, context):
"""
Converts a value as returned by the database to a Python object. It is like the reverse of
get_prep_value().
:param value: the array as a json formatted string
:type: str
:return: value, as numpy 1-dimensional array
:type: np.ndarray
"""
return self.to_python(value)
@Vichoko
Copy link

Vichoko commented May 2, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment