thanks a lot guys!

On Mon, Sep 15, 2008 at 6:10 PM, Jirka Vejrazka <[EMAIL PROTECTED]>wrote:

>
> Hi, this is how I do it, but don't rely on effectiveness of this
> solution - I'd be the first one to say that I'm a mediocre programmer
> :)
>
> def serialize(data):
>   '''Input: instance of PluginParameters
>      Output: base64-encoded string that can be stored in a database
>   '''
>   if not isinstance(data, PluginParameters):
>       error('Attempt to serialize data of unsupported type (%s)' %
> type(data))
>       raise TypeError('Cannot serialize data of type %s' % type(data))
>   # Loop through all attributes, identify any QuerySets
>   # and use only their 'query' attribute for pickling (disregard the rest)
>   for attr_name in data.__dict__:
>       value = getattr(data, attr_name)
>       if isinstance(value, QuerySet):
>           setattr(data, attr_name, value.query)
>   if _compress:
>       res = b64encode(zlib.compress(pickle.dumps(data)))
>   else:
>       res = b64encode(pickle.dumps(data))
>   debug2('Serialized parameters: %g bytes' % len(res))
>   return res
>
> def deserialize(data):
>   '''process base64 encoded pickled object
>   and return an instance of PluginParameters
>   '''
>   debug2('Deserializing parameters: %g bytes' % len(data))
>   if _compress:
>       res = pickle.loads(zlib.decompress(b64decode(data)))
>   else:
>       res = pickle.loads(b64decode(data))
>   # identify any 'query' object and reconstruct the original QuerySet
>   for attr_name in res.__dict__:
>       value = getattr(res, attr_name)
>       if isinstance(value, Query):
>           # the black magic starts here ;-)
>           model = value.model  # get the original model owning the query
>           qs = model.objects.all()  # create a QuerySet based on that model
>           qs.query = value  # reattach the orignal 'query' to the QuerySet
>           setattr(res, attr_name, qs)  # and inject this back to the
>                                        # PluginParameters object
>   return res
>
> Hope this helps
>
>  Jirka
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to