Skip to main content

Serialize/Deserialize an object and its relations through class definitions

Project description

django-deep-serializer

https://travis-ci.org/goinnn/django-deep-serializer.png?branch=master https://coveralls.io/repos/goinnn/django-deep-serializer/badge.png?branch=master https://badge.fury.io/py/django-deep-serializer.png https://pypip.in/d/django-deep-serializer/badge.png

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

Requeriments

  • 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)

Installation

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:

SERIALIZATION_MODULES = {
    "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 “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 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):

    @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 in moocng project

Development

You 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-serializer

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 manage.py syncdb --noinput
python manage.py loaddata app_data.json
python manage.py runserver

Releases

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


Download files

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

Source Distribution

django-deep-serializer-0.1.3.tar.gz (22.8 kB view details)

Uploaded Source

File details

Details for the file django-deep-serializer-0.1.3.tar.gz.

File metadata

File hashes

Hashes for django-deep-serializer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7054f4d51f345748a52d2b867431040657421de74c6d2c8623082fdfb7d418d0
MD5 532d138549bff2e4b73a1ce25a08d690
BLAKE2b-256 379a5a2221c42bdc42ccf5b7621b72b6fd48023e0fee224f1ff725c2157c12f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page