Jason Christa Jason Christa - 1 year ago 130
JSON Question

How do you serialize a model instance in Django?

There is a lot of documentation on how to serialize a Model QuerySet but how do you just serialize to JSON the fields of a Model Instance?

Answer Source

You can easily use a list to wrap the required object and that's all what django serializers need to correctly serialize it, eg.:

from django.core import serializers

# assuming obj is a model instance
serialized_obj = serializers.serialize('json', [ obj, ])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download