Last active
August 29, 2015 14:28
-
-
Save dpwrussell/7a72fee067a40f4dca97 to your computer and use it in GitHub Desktop.
colanderalchemy_question_sideload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Integer | |
from sqlalchemy import Column | |
from sqlalchemy import String | |
from sqlalchemy import ForeignKey | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker, relationship | |
from sqlalchemy.ext.declarative import declarative_base | |
import colander | |
from sqlalchemy import event | |
from sqlalchemy.orm import mapper | |
from colanderalchemy import setup_schema | |
import pprint | |
event.listen(mapper, 'mapper_configured', setup_schema) | |
Base = declarative_base() | |
class Person(Base): | |
__tablename__ = 'person' | |
id = Column(Integer, primary_key=True) | |
name = Column(String(128)) | |
surname = Column(String(128)) | |
class Score(Base): | |
__tablename__ = 'score' | |
# __colanderalchemy_config__ = { | |
# 'excludes': ['person_id', 'person', 'value'] | |
# } | |
id = Column(Integer, primary_key=True) | |
person_id = Column(Integer, ForeignKey('person.id')) | |
person = relationship('Person', backref='scores') | |
value = Column(Integer) | |
engine = create_engine('sqlite:///:memory:', echo=False) | |
Base.metadata.create_all(engine) | |
Session = sessionmaker(bind=engine) | |
session = Session() | |
person = Person(name='Jayne', surname='Cobb') | |
session.add(person) | |
score1 = Score(value=1000, person=person) | |
score2 = Score(value=2000, person=person) | |
session.add(score1) | |
session.add(score2) | |
session.commit() | |
# schema = Score.__colanderalchemy__ | |
# print 'Score 1' | |
# pprint.pprint(schema.serialize(schema.dictify(score1))) | |
# print 'Score 2' | |
# pprint.pprint(schema.serialize(schema.dictify(score2))) | |
schema = Person.__colanderalchemy__ | |
print 'Person' | |
pprint.pprint(schema.serialize(schema.dictify(person))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment