Last active
August 29, 2015 14:28
-
-
Save dpwrussell/f465aee4618580614f61 to your computer and use it in GitHub Desktop.
colanderalchemy_question_inheritance_backref
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' | |
satype = Column(String(50)) | |
__mapper_args__ = { | |
'polymorphic_identity': 'person', | |
'polymorphic_on': satype | |
} | |
__colanderalchemy_config__ = { | |
'excludes': ['surname'] | |
} | |
id = Column(Integer, primary_key=True) | |
name = Column(String(128)) | |
surname = Column(String(128)) | |
a_number = Column(Integer(), info={ | |
'colanderalchemy': { | |
'typ': colander.Float() | |
} | |
} | |
) | |
class Gamer(Person): | |
__tablename__ = 'gamer' | |
__mapper_args__ = { | |
'polymorphic_identity': 'gamer', | |
} | |
__colanderalchemy_config__ = { | |
'excludes': ['satype'] | |
} | |
id = Column(Integer, ForeignKey('person.id'), primary_key=True) | |
high_score = Column(Integer) | |
# class Gamer(Base): | |
# __tablename__ = 'gamer' | |
# id = Column(Integer, primary_key=True) | |
# name = Column(String(128)) | |
# surname = Column(String(128)) | |
# high_score = Column(Integer) | |
class Score(Base): | |
__tablename__ = 'score' | |
id = Column(Integer, primary_key=True) | |
gamer_id = Column(Integer, ForeignKey('gamer.id')) | |
gamer = relationship('Gamer', backref='scores') | |
value = Column(Integer) | |
# class Score(Base): | |
# __tablename__ = 'score' | |
# id = Column(Integer, primary_key=True) | |
# gamer_id = Column(Integer, ForeignKey('person.id')) | |
# gamer = 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() | |
gamer = Gamer(name='Jayne', surname='Cobb', a_number=123) | |
session.add(gamer) | |
score1 = Score(value=1000, gamer=gamer) | |
score2 = Score(value=2000, gamer=gamer) | |
session.add(score1) | |
session.add(score2) | |
session.commit() | |
# Check that the backref is functioning (it is): | |
print 'Scores', [score.value for score in gamer.scores] | |
# Print a score, the relationship mapping is used to display the gamer | |
# serialization inline | |
schema = Score.__colanderalchemy__ | |
print 'Score 1' | |
pprint.pprint(schema.serialize(schema.dictify(score1))) | |
# Print the gamer. The relationship backref is not used to display the | |
# serializations of the scores inline | |
schema = Gamer.__colanderalchemy__ | |
print 'Gamer' | |
pprint.pprint(schema.serialize(schema.dictify(gamer))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment