Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Serialize/Deserialize an object and its relations through class definitions

Project description


With django-deep-serializer you can serialize/deserialize an object and its relations through class definitions


  • django (>=1.4, it’s possible that works with previous versions)
  • PyYAML (>=3.10, optional only if you want use this serializer)
  • django-form-admin (>=0.4.2, optional only to the example project)


If you want use natural keys, you have use the internal serializers These are got from django git repository. These are not in the any stable branch or release. You have to write in your settings:

    "xml"    : "deep_serializer.serializers.xml_serializer",
    "python" : "deep_serializer.serializers.python",
    "json"   : "deep_serializer.serializers.json",
    #"yaml"   : "deep_serializer.serializers.pyyaml",

Use cases

  • Serialize (using primary keys or natural keys) an object and its relations. Sometimes django can not serialize an app. E.g. if you try to serialize the “” application in the example project you will get the next error: “CommandError: Unable to serialize database: Can’t resolve dependencies for app.Page, app.WebSite in serialized app list.”
  • Deserialize (using primary keys or natural keys) some objects
  • Clone (using natural keys) an object. To do you can serialize, update the natural key to the main object and after deserialize these objects
  • Restore an object with its relations, (using primary keys or natural keys)

How to use

The idea is get to have a serializer or/and a deserializer implemented with very few lines. These have to be able to define some “rules”. There are five examples (five distinct use case) in the example project. E.g.:

class WebSiteClone(MyMetaWalkClass):

    def pre_serialize(cls, initial_obj, obj, request, serialize_options=None):
        obj = super(WebSiteClone, cls).pre_serialize(initial_obj, obj,
        new_title = '%s-%s' % (obj.title, time.time())
        obj.title = new_title[:200]
        obj.slug = get_hash()
        obj.original_website_id =
        obj.initial_page = None
        return obj

    def walking_into_class(cls, initial_obj, obj, field_name, model, request=None):
        if field_name in ('initial_page', 'websites_created_of'):
            return WALKING_STOP
        elif field_name in ('original_website', 'owners'):
            return ONLY_REFERENCE
        elif field_name == 'page':
            return WALKING_INTO_CLASS
        update_the_serializer(obj, field_name)

class PageClone(MyMetaWalkClass):

    def pre_serialize(cls, initial_obj, obj, request, serialize_options=None):
        obj = super(PageClone, cls).pre_serialize(initial_obj,
                                                obj, request,
                                                serialize_options=serialize_options) = initial_obj
        obj.created_from_id =
        return obj

    def walking_into_class(cls, initial_obj, obj, field_name, model, request=None):
        if field_name in ('pages_created_of', 'website', 'website_initial_page'):
            return WALKING_STOP
        elif field_name in ('created_from', 'last_editor'):
            return ONLY_REFERENCE
        update_the_serializer(obj, field_name)

    def post_save(cls, initial_obj, obj, request=None):
        super(PageClone, cls).post_save(initial_obj, obj, request=request)
        initial_page =
        if initial_page and obj.slug == initial_page.slug:
   = obj

def clone_website(website, format='python'):
    walking_classes = {WebSite: WebSiteClone,
                       Page: PageClone,
                       User: BaseMetaWalkClass}
    natural_keys = True
    fixtures = serializer(format, website,
    return deserializer(format, fixtures,

You can see a real example in moocng project


You can get the last bleeding edge version of django-deep-serializer by doing a clone of its git repository:

git clone

Test project

In the source tree, you will find a directory called ‘example’. It contains a readily setup project that uses django-deep-serializer. You can run it as usual:

python syncdb --noinput
python loaddata app_data.json
python runserver


0.1.3 (2014-10-13)

  • Support to Django 1.7

0.1.2 (2013-11-14)

  • Little details

0.1.1 (2013-11-13)

  • Add tests to django 1.6
  • Update the django 1.5 version, from 1.5.4 to 1.5.5

0.1.0 (2013-11-05)

  • Fix a bug when you deserializer and the format to the fixtures was XML
  • Add message when raise exceptions
  • Improvements in the serialization form in the example project
  • Fix a little error when you serialized and the format was python
  • Reorder and refactor the code. There are backward incompatible changes like reorder of the params of the methods
  • Improvements in the tests

0.0.1 (2013-10-29)

  • Initial version

Project details

Release history Release notifications

This version
History Node


History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django-deep-serializer-0.1.3.tar.gz (22.8 kB) Copy SHA256 hash SHA256 Source None Oct 13, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page