From 7a0bd1c4d1950f49d8e0710f535d3d15c2c43d79 Mon Sep 17 00:00:00 2001 From: Dave Allan Date: Wed, 26 Apr 2017 12:00:32 -0400 Subject: [PATCH] Use text area for JSON fields and indent JSON --- rest_framework/utils/field_mapping.py | 4 ++-- rest_framework/utils/serializer_helpers.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index b8817d976..cc82492dc 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -8,7 +8,7 @@ from django.core import validators from django.db import models from django.utils.text import capfirst -from rest_framework.compat import DecimalValidator +from rest_framework.compat import DecimalValidator, JSONField from rest_framework.validators import UniqueValidator NUMERIC_FIELD_TYPES = ( @@ -88,7 +88,7 @@ def get_field_kwargs(field_name, model_field): if decimal_places is not None: kwargs['decimal_places'] = decimal_places - if isinstance(model_field, models.TextField): + if isinstance(model_field, models.TextField) or (JSONField and isinstance(model_field, JSONField)): kwargs['style'] = {'base_template': 'textarea.html'} if isinstance(model_field, models.AutoField) or not model_field.editable: diff --git a/rest_framework/utils/serializer_helpers.py b/rest_framework/utils/serializer_helpers.py index 67b89634c..2c4407d74 100644 --- a/rest_framework/utils/serializer_helpers.py +++ b/rest_framework/utils/serializer_helpers.py @@ -85,7 +85,7 @@ class BoundField(object): class JSONBoundField(BoundField): def as_form_field(self): - value = json.dumps(self.value) + value = json.dumps(self.value, sort_keys=True, indent=4) return self.__class__(self._field, value, self.errors, self._prefix)