diff --git a/ndarraydjango/fields.py b/ndarraydjango/fields.py index 497c9a6..3c8e49d 100644 --- a/ndarraydjango/fields.py +++ b/ndarraydjango/fields.py @@ -80,6 +80,13 @@ def from_db_value(self, value, expression, connection): return parse_numpy_array(value) def validate(self, value, model_instance): + + if ( + ( value is None and self.blank ) or + ( value is None and self.null ) + ): + return + if self.shape is not None and value.shape != self.shape: raise exceptions.ValidationError("Bad shape", code="shape") @@ -103,6 +110,11 @@ def to_python(self, value): return value if isinstance(value, str): + if ( + ( value=='' and self.blank ) or + ( value=='' and self.null ) + ): + return None try: value = np.array(json.loads(value), dtype=self.dtype) except json.decoder.JSONDecodeError: diff --git a/ndarraydjango/forms.py b/ndarraydjango/forms.py index 1b42156..94a3b8e 100644 --- a/ndarraydjango/forms.py +++ b/ndarraydjango/forms.py @@ -30,6 +30,8 @@ def prepare_value(self, value): def to_python(self, value): value = super().to_python(value) + if (value==self.empty_value) and (not self.required): + return value try: value = json.loads(value) except json.decoder.JSONDecodeError: