Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework and JSONField

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.

like image 579
Tzach Avatar asked Mar 16 '14 08:03

Tzach


People also ask

What is JSONField Django?

One of the cool features that was introduced in Django 3.1 was the JSONField . JSONField allows for you to store semi-structured data alongside other data fields in PostgreSQL, MySQL, and SQLite databases and supports introspection, lookups, and transforms.

Is Django different from Django REST framework?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.

Can I use Django and Django REST framework together?

Django Rest Framework makes it easy to use your Django Server as an REST API. REST stands for "representational state transfer" and API stands for application programming interface. Note that with DRF you easily have list and create views as well as authentication.

Is Django rest framework a framework?

Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.


2 Answers

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

like image 94
Mark Chackerian Avatar answered Oct 12 '22 12:10

Mark Chackerian


In 2.4.x:

from rest_framework import serializers # get from https://gist.github.com/rouge8/5445149  class WritableJSONField(serializers.WritableField):     def to_native(self, obj):         return obj   class MyModelSerializer(serializers.HyperlinkedModelSerializer):     my_json_field = WritableJSONField() # you need this. 
like image 37
gzerone Avatar answered Oct 12 '22 14:10

gzerone