Django Rest Framework and JSONField

2019-01-08 16:34发布

Given a Django model with a JSONField, what is the correct way of serializing and deserializing it using Django Rest Framework?

I've already tried crating a custom serializers.WritableField and overriding to_native and from_native:

from json_field.fields import JSONEncoder, JSONDecoder
from rest_framework import serializers

class JSONFieldSerializer(serializers.WritableField):
    def to_native(self, obj):
    return json.dumps(obj, cls = JSONEncoder)

    def from_native(self, data):
        return json.loads(data, cls = JSONDecoder)

But when I try to updating the model using partial=True, all the floats in the JSONField objects become strings.

10条回答
做个烂人
2楼-- · 2019-01-08 16:47

For the record, this "just works" now if you are using PostgreSQL, and your model field is adjango.contrib.postgres.JSONField.

I'm on PostgreSQL 9.4, Django 1.9, and Django REST Framework 3.3.2.

I have previously used several of the other solutions listed here, but was able to delete that extra code.

Example Model:

class Account(models.Model):
    id = UUIDField(primary_key=True, default=uuid_nodash)
    data = JSONField(blank=True, default="")

Example Serializer:

class AccountSerializer(BaseSerializer):
    id = serializers.CharField()
    class Meta:
        model = Account
        fields = ('id','data')

Example View:

class AccountViewSet(
    viewsets.GenericViewSet,
    mixins.CreateModelMixin,      
    mixins.RetrieveModelMixin,
    mixins.ListModelMixin,
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin
): 
    model = Account
    queryset = Account.objects.all()
    serializer_class = AccountSerializer
    filter_fields = ['id', 'data']
查看更多
Juvenile、少年°
3楼-- · 2019-01-08 16:50

If you're using mysql (haven't tried with other databases), using both DRF's new JSONField and Mark Chackerian's suggested JSONSerializerField will save the json as a {u'foo': u'bar'} string. If you rather save it as {"foo": "bar"}, this works for me:

import json

class JSONField(serializers.Field):
    def to_representation(self, obj):
        return json.loads(obj)

    def to_internal_value(self, data):
        return json.dumps(data)
查看更多
Evening l夕情丶
4楼-- · 2019-01-08 16:51

Mark Chackerian script didn't work for me, I'd to force the json transform:

import json

class JSONSerializerField(serializers.Field):
    """ Serializer for JSONField -- required to make field writable"""

    def to_internal_value(self, data):
        json_data = {}
        try:
            json_data = json.loads(data)
        except ValueError, e:
            pass
        finally:
            return json_data
    def to_representation(self, value):
        return value

Works fine. Using DRF 3.15 and JSONFields in Django 1.8

查看更多
Deceive 欺骗
5楼-- · 2019-01-08 16:57

DRF gives us inbuilt field 'JSONField' for binary data, but JSON payload is verified only when you set 'binary' flag True then it convert into utf-8 and load the JSON payload, else it only treat them as string(if invalid json is sent) or json and validate both without error even though you cretaed JSONField

class JSONSerializer(serializers.ModelSerializer):
    """
    serializer for JSON
    """
    payload = serializers.JSONField(binary=True)
查看更多
够拽才男人
6楼-- · 2019-01-08 16:59

If you're using Django Rest Framework >= 3.3, then the JSONField serializer is now included. This is now the correct way.

If you're using Django Rest Framework < 3.0, then see gzerone's answer.

If you're using DRF 3.0 - 3.2 AND you can't upgrade AND you don't need to serialize binary data, then follow these instructions.

First declare a field class:

from rest_framework import serializers

class JSONSerializerField(serializers.Field):
    """ Serializer for JSONField -- required to make field writable"""
    def to_internal_value(self, data):
        return data
    def to_representation(self, value):
        return value

And then add in the field into the model like

class MySerializer(serializers.ModelSerializer):
    json_data = JSONSerializerField()

And, if you do need to serialize binary data, you can always the copy official release code

查看更多
Deceive 欺骗
7楼-- · 2019-01-08 17:03

If and only if you know the first-level style of your JSON content (List or Dict), you can use DRF builtin DictField or ListField.

Ex:

class MyModelSerializer(serializers.HyperlinkedModelSerializer):
    my_json_field = serializers.DictField()

It works fine, with GET/PUT/PATCH/POST, including with nested contents.

查看更多
登录 后发表回答