Thanks, it works great, but only for pickle protocol 2:
slight modification: cls.get instead of cls.getByID

class MySqlObject(SQLObject):
   def __getstate__(self):
       return self.id
   def __setstate__(self,id):
       pass
   def __new__(cls, *args, **kw):
       if len(args) == 1:
           return cls.get(args[0])
       return SQLObject.__new__(cls, *args, **kw)

   def __getnewargs__(self):
       return (self.id,)

basic test:
 >>> import cPickle
 >>> a=Users.get(1)
 >>> b=cPickle.dumps(a,2)
 >>> c=cPickle.loads(b)
 >>> c
<Users 1 login='asdf' password='qwerqwer' email="'[EMAIL PROTECTED]'" 
validated=True validationString='0sdr8z6kfj'>
 >>> a is c
True
 >>> a == c
True
 >>> id(a)
-1222564404
 >>> id(c)
-1222564404
 >>>

Luke Opperman wrote:

>I don't recall whether getstate/setstate are necessary - i think so, just for
>the protocol. the key is overriding new/getnewargs. i also don't recall how
>generally applicable this is, regarding different connection schemes etc.
>
>     ## Pickle protocol hacks
>
>     def __new__(cls, *args, **kw):
>         if len(args) == 1:
>             return cls.getByID(args[0])
>         return SQLObject.__new__(cls, *args, **kw)
>
>     def __getnewargs__(self):
>         return (self.id,)
>
>     def __getstate__(self):
>         return self.id
>
>     def __setstate__(self, state):
>         ''' handled by __new__/__getnewargs__ '''
>         pass
>
>     ## End pickle protocol hacks
>
>
>
>
>_______________________________________________
>sqlobject-discuss mailing list
>[email protected]
>https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
>
>  
>



_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to