Package: django-deep-serializer
Contentsdjango-deep-serializerRequerimentsInstallationUse casesHow to useDevelopmentTest projectReleases0.1.3 (2014-10-13)0.1.2 (2013-11-14)0.1.1 (2013-11-13)0.1.0 (2013-11-05)0.0.1 (2013-10-29)django-deep-serializerWith django-deep-serializer you can serialize/deserialize an object and its relations through class definitionsRequerimentsdjango(>=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)InstallationIf you want use natural keys, you have use theinternal serializersThese are got from django git repository. These are not in the any stable branch or release. You have to write in your settings:SERIALIZATION_MODULES = {
    "xml"    : "deep_serializer.serializers.xml_serializer",
    "python" : "deep_serializer.serializers.python",
    "json"   : "deep_serializer.serializers.json",
    #"yaml"   : "deep_serializer.serializers.pyyaml",
}Use casesSerialize (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 “example.app” 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 objectsClone (using natural keys) an object. To do you can serialize, update the natural key to the main object and after deserialize these objectsRestore an object with its relations, (using primary keys or natural keys)How to useThe 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 theexample project. E.g.:class WebSiteClone(MyMetaWalkClass):

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

    @classmethod
    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):

    @classmethod
    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)
        obj.website = initial_obj
        obj.created_from_id = obj.pk
        return obj

    @classmethod
    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)

    @classmethod
    def post_save(cls, initial_obj, obj, request=None):
        super(PageClone, cls).post_save(initial_obj, obj, request=request)
        initial_page = obj.created_from.website.initial_page
        if initial_page and obj.slug == initial_page.slug:
            obj.website.initial_page = obj
            obj.website.save()


def clone_website(website, format='python'):
    walking_classes = {WebSite: WebSiteClone,
                       Page: PageClone,
                       User: BaseMetaWalkClass}
    natural_keys = True
    fixtures = serializer(format, website,
                          walking_classes=walking_classes,
                          natural_keys=natural_keys)
    return deserializer(format, fixtures,
                        initial_obj=website,
                        walking_classes=walking_classes,
                        natural_keys=natural_keys)You can see a real example inmoocng projectDevelopmentYou can get the last bleeding edge version of django-deep-serializer by doing a clone
of its git repository:git clone https://github.com/goinnn/django-deep-serializerTest projectIn 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 manage.py syncdb --noinput
python manage.py loaddata app_data.json
python manage.py runserverReleases0.1.3 (2014-10-13)Support to Django 1.70.1.2 (2013-11-14)Little details0.1.1 (2013-11-13)Add tests to django 1.6Update the django 1.5 version, from 1.5.4 to 1.5.50.1.0 (2013-11-05)Fix a bug when you deserializer and the format to the fixtures was XMLAdd message when raise exceptionsImprovements in the serialization form in the example projectFix a little error when you serialized and the format was pythonReorder and refactor the code.There are backward incompatible changeslike reorder of the params of the methodsImprovements in the tests0.0.1 (2013-10-29)Initial version