Skip to content

Instantly share code, notes, and snippets.

@chadwhitacre
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadwhitacre/9175133 to your computer and use it in GitHub Desktop.
Save chadwhitacre/9175133 to your computer and use it in GitHub Desktop.
"""Here's how you implement Foo(pkid) with postgres.orm 2.1.x.
Discovered at PyTennesse with @wlphoenix @PederSchacht et al.
"""
class Foo(Model):
typname = 'foo'
def __new__(cls, pkid_or_record):
if type(pkid_or_record) is int:
obj = cls.db.one( 'select foo.*::foo from foo where pkid=%s'
, (pkid_or_record,)
)
return obj
else:
return super().__new__(cls) # This is the Python 3 version of super.
def __init__(self, record):
if type(record) is dict:
Model.__init__(self, record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment