django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

JSONField not rendered properly in DRF browsable API HTML form

Open hommel opened this issue 9 years ago • 2 comments

Checklist

  • [x] I have verified that that issue exists against the master branch of Django REST framework.
    • 73ad88eaae2f49bfd09508f2dcd6446677800a26
  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
    • #4135: webform-input parsed as string (PR: #4565)
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [x] I have reduced the issue to the simplest possible case.
  • [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Expected behavior

Assuming:

  • a valid JSON object is stored in postgreSQL as a django models.JSONField and
  • that data is serialized using DRF's serializers.JSONField,

then that data should render as a JSON object in DRF browsable API (particularly because the serializer requires input to be valid JSON for POST/PUT/PATCH methods)

Actual behavior

Data renders as native python in DRF browsable API form field (e.g. single quotes, uppercase booleans)

instance_view_html

Note that the "Raw data" view of the input section renders the data correctly as JSON.

instance_view_raw

Steps to reproduce

(venv)vagrant@vm:/vagrant/src/app$ python -V
Python 3.4.3
(venv)vagrant@vm:/vagrant/src/app$ psql -V
psql (PostgreSQL) 9.6.2
# requirements.txt
Django==1.10.6
psycopg2==2.7.1
-e [email protected]:tomchristie/django-rest-framework.git@73ad88eaae2f49bfd09508f2dcd6446677800a26#egg=djangorestframework-origin/HEAD
# models.py

from django.db import models
from django.contrib.postgres.fields import JSONField


class Foo(models.Model):

    name = models.CharField(max_length=24)
    data = JSONField()
# serializers.py

from rest_framework import serializers

from jsontest import models


class FooSerializer(serializers.HyperlinkedModelSerializer):

    name = serializers.CharField(max_length=24)
    data = serializers.JSONField()

    class Meta:
        model = models.Foo
        fields = ('url', 'name', 'data')
>>> from jsontest.models import *
>>> f = Foo()
>>> f.name = 'created via shell'
>>> f.data = {'input': 'native dict', 'bar': True}
>>> f.save()
>>> f.id
1

hommel avatar Mar 17 '17 21:03 hommel

Thanks for reporting. I gave it a try and yeah, this is reproduced.

xordoquy avatar Mar 18 '17 05:03 xordoquy

~~I'm still experiencing this bug.~~ Edit: issue #6211 is separate but related.

django==2.0.5
djangorestframework==3.8.2

aleehedl avatar Sep 25 '18 09:09 aleehedl