din's blog

An alternative model serializer for django

Django's serializer has some limitations which makes it a bit of a pain to use. Basically it will ignore any atributes that have been added to a model object.

The code below is for an alternative serializer. This version allows you select what attributes will be serialized on a per object basis. It also allows you to either serialize the data into json or xml. This code also works if you have model instance as an attribute of another model.

You can get the code on django snippets.

The original json encoder was written by Wolfram Kriesing.

Usage:

dumper = DataDumper()
dumper.selectObjectFields('model_class_name',[...fields...])
dumper.selectObjectFields('model_class_name',[...fields...])
dumper.dump(model_instance,'xml')
dumper.dump(model_instance,'json')
dumper.dump(queryset,'xml')


Tags:

We design and develop iPhone Apps. We focus on true native cocoa Mac and iPhone application development.

We also provide website development using Drupal, and Django.