How do you serialize a model instance in Django?

Jason Christa picture Jason Christa · Apr 16, 2009 · Viewed 155.4k times · Source

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

xaralis picture xaralis · Jul 20, 2010

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, ])