Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django serializer for one object

Tags:

I'm trying to figure out a way to serialize some Django model object to JSON format, something like:

j = Job.objects.get(pk=1) ############################################## #a way to get the JSON for that j variable??? ############################################## 

I don't want:

from django.core import serializers serializers.serialize('json', Job.objects.get(pk=1),ensure_ascii=False) 

Because it returns JSON array, not a single object representation.

Any ideas?

One way I'm thinking of: is to find a way to get a hash(attribute,value) of the object and then use simplejson to get the JSON representation of it, however I don't know how to get that hash.

like image 927
khelll Avatar asked Mar 06 '10 01:03

khelll


People also ask

Is it necessary to use Serializers in Django?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.

What is serializing Django?

Django's serialization framework provides a mechanism for “translating” Django models into other formats. Usually these other formats will be text-based and used for sending Django data over a wire, but it's possible for a serializer to handle any format (text-based or not).

What is deserializer in Django?

deserialize is for deserializing a particular type of JSON - that is, data that was serialized from model instances using serializers. serialize . For your data, you just want the standard simplejson module.


2 Answers

How about just massaging what you get back from serializers.serialize? It is not that hard to trim off the square brackets from the front and back of the result.

job = Job.objects.get(pk=1) array_result = serializers.serialize('json', [job], ensure_ascii=False) just_object_result = array_result[1:-1] 

Not a fancy answer but it will give you just the object in json notation.

like image 68
istruble Avatar answered Nov 05 '22 16:11

istruble


Method-1

Use Django Serializer with python format

from django.core import serializers  j = Job.objects.get(pk=1) response = serializers.serialize('python', [j], ensure_ascii=False)

Method-2

use json format while serializing and loads the string response

import json from django.core import serializers  j = Job.objects.get(pk=1) json_str_response = serializers.serialize('json', [j], ensure_ascii=False) response = json.loads(json_str_response)[0]

Method-3

Use Django REST Framework's Serializer class define a serializer class and serialize the instance as

from rest_framework import serializers   class JobSerializer(serializers.ModelSerializer):     class Meta:         model = Job         fields = '__all__'   j = Job.objects.get(pk=1) response = JobSerializer(instance=j).data 

Reference
1. Serializer Django model object

like image 24
JPG Avatar answered Nov 05 '22 14:11

JPG