Skip to content

JSONField not rendered properly in DRF browsable API HTML form #4999

@hommel

Description

@hommel

Checklist

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 git+git@github.com: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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy