By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Understanding django serializers with examples

Using Serializers,  we can translate Django model objects into other formats like XML, JSON, YAML(YAML Ain’t a Markup Language) with a single command.

from django.core import serializers

data = serializers.serialize("xml", SomeModel.objects.all())

the first argument will be the format we want to serialize the data and second will be a QuerySet to serialize.

we can also translate using a serializer object directly

XMLSerializer = serializers.get_serializer("xml")

xml_serializer = XMLSerializer()

xml_serializer.serialize(queryset)

data = xml_serializer.getvalue()

this is useful when we want data directly be a file like object.

we can also translate only subset of fields using fields argument 

from django.core import serializers

data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size')) class Teacher(models.Model):

    name = models.CharField(max_length=50)

    qualification = models.CharField(max_length=50)        

class College(Teacher):

    name = models.models.CharField(max_length=50)

for the inherited models,to translate child class objects we have to list all parent class objects and child class objects into single list,then translate.

all_objects = list(Teacher.objects.all()) + list(College.objects.all())

data = serializers.serialize('xml', all_objects)

Deserializing data:

for obj in serializers.deserialize("xml", data):
    do_something_with(obj)

it will take same arguments as serialize function and returns an iterator

The objects returned by the deserialize iterator are unsavedobjects and Calling DeserializedObject.save() saves the object to the database.


for deserialized_object in serializers.deserialize("xml", data):

    if object_should_be_saved(deserialized_object):

        deserialized_object.save()
    Posted On
  • 14 August 2013
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Basics of Django templates

Django template engine comes as part of django framework and its very easy, extensible and handy to develop complex web applications with simplest efforts. Lets ...

Continue Reading...
How to use nested formsets in django

Django Formsets manage the complexity of multiple copies of a form in a view. By using formsets, you can know how many forms were their ...

Continue Reading...
Set Up Travis CI For Django project

Travis CI is a continuous integration service used to build and test applications hosted at GitHub. Here are simple steps to add CI to you ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties