Created
January 9, 2015 23:24
-
-
Save imhoffd/e6a3a061fb3badccd5da to your computer and use it in GitHub Desktop.
sqlalchemy polymorphic load_only
This file contains hidden or 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
# no | |
query = session.query(MySubModel) | |
query = query.options(load_only("id", "type")) | |
# yes, but does not filter type of model | |
query = session.query(MyModel).with_polymorphic(MySubModel) | |
query = query.options(load_only("id", "type")) | |
# yes, but ew | |
query = session.query(MyModel) | |
query = query.options(load_only("id", "type")) | |
query = query.filter(MyModel.type == "my_sub_model") |
This file contains hidden or 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
MyModel(Base): | |
__table_name__ = "my_table" | |
id = Column(Int, primary_key=True) | |
name = Column(Text) | |
type = Column(Text) | |
__mapper_args__ = {'polymorphic_identity': 'my_model', | |
'polymorphic_on': type} | |
MySubModel(MyModel): | |
__mapper_args__ = {'polymorphic_identity': 'my_sub_model'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment