Serialize/Deserialize an object and its relations through class definitions
Project description
django-deep-serializer
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
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} fixtures = serializer(format, website, request=None, walking_classes=walking_classes, natural_keys=natural_keys) return deserializer(format, website, fixtures, request=None, walking_classes=walking_classes, natural_keys=natural_keys)
You can see a real example in moocng project
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 runserver
Releases
0.0.1 (2013-10-28)
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
File details
Details for the file django-deep-serializer-0.0.1.tar.gz
.
File metadata
- Download URL: django-deep-serializer-0.0.1.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c57eb01265e1a75833fa6d60011e79cab7f8d86fd0f1ddc75007ee2372cb24c2 |
|
MD5 | d2204edfbfef2038251a4cb21ca0d705 |
|
BLAKE2b-256 | 6145e054d5a4eccda518b7eb0f5a39bfe1518b8ce499469416d9de92752a7a47 |