mongoengine
mongoengine copied to clipboard
Serialization / deserialization
Create a serializer and deserializer for mongodb documents...
Useful for testing / small data exporting..
Refs: #181
from #230
example:
#serializing Person.objects.serialize('xml') Person.objects.serialize('yaml')
a = open('output.xml', 'wb') Person.objects.serialize('xml', fields=('data.name', 'endr'), out=a)
#deserialize data = open('input.xml', 'rb').read() objs = Person.objects.deserialize('xml', data)
for obj in objs; obj.save()
IMO it'd be cool to have serialization/deserialization helper classes instead hooking it with queryset. My examples are inspired from django project.
data = serializers.serialize("xml", SomeModel.objects.all())
data = serializers.serialize("json", SomeModel.objects.all(), excludes=('password',))
def get_absolute_url(self): #document class method
return "/user/%d" % self.pk
data = serializers.serialize("json", SomeModel.objects.all(), excludes=('password',),
extras=[('url', 'get_absolute_url'),])
for deserialized_object in serializers.deserialize("xml", data):
if object_should_be_saved(deserialized_object):
deserialized_object.save()
If someone try to load partial data into deserializer, it should work until it's valid but may not able to save it if constraint fails.
@rozza. I shall work on that feature in the next days. is a very important feature for various projects.
I am starting work in this branch: https://github.com/wpjunior/mongoengine/tree/serializers
Example Encoder - https://gist.github.com/1372780
Great, thanks for the pointer. Updated version to use json over simplejson, and dbref over pymongo:
https://gist.github.com/2726279
I had to make a serializer for the app i'm working on. It surely requires some improvements, but it seems to work. It serializes querysets / lists of querysets. I wouldn't use it to serialize huge datasets however.
https://gist.github.com/3009143
Good stuff! I after europython I will have time look into this.
Ross
On Thu, Jun 28, 2012 at 5:31 AM, benwah < [email protected]
wrote:
FYI.. the reason it's kinda long, is that I wanted to serialize things like lists of querysets, for json views, like so:
[AdCategory.objects.all(), Region.objects.all()]
I could do it with a simple serializer and string formatting, but I though building json using string formatting wasn't especially pretty.
Reply to this email directly or view it on GitHub: https://github.com/hmarr/mongoengine/issues/210#issuecomment-6621706
Any progress on this?
+1
0.8 is underway :)
On Tue, Oct 16, 2012 at 2:37 PM, Honza Javorek [email protected]:
+1
— Reply to this email directly or view it on GitHubhttps://github.com/hmarr/mongoengine/issues/210#issuecomment-9488088.
+1
+1
I've made a MongoEngine Model Serializer , powered by Django Rest Framework give it a try https://github.com/umutbozkurt/mongoengine-serializer