Skip to content

Instantly share code, notes, and snippets.

@malthe

malthe/pickle.py Secret

Created March 28, 2014 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malthe/b03cad86c4f9c4382045 to your computer and use it in GitHub Desktop.
Save malthe/b03cad86c4f9c4382045 to your computer and use it in GitHub Desktop.
def get_instance(cls, args):
inst = cls.query.get_raw(args)
if inst is None:
raise ValueError(
"Query for type '%s' returned no items for "
"primary key: (%s)." % (
cls.__name__,
", ".join(map(repr, args))
))
return inst
def get_instance_query(inst):
return (
get_instance,
(type(inst), tuple(getattr(inst, name) for name in (
column.name for column in inst.__mapper__.primary_key
))))
Model.__reduce__ = get_instance_query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment