Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

python - How can I set Polymorphic Identity after a class is defined?

I have a Class defining a table with Mapper Argument: polymorphic_on: column

I have some other Classes inhering this class and they all need to set __mapper_args__ 'polymorphic_identity', which I would like to do after a class is defined instead of having it set within the Class definition.

How can I set polymorphic_identity Mapper Argument after a class is defined?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this is not totally public API but should work:

from sqlalchemy.orm import class_mapper
mapper = class_mapper(MySubClass)
mapper.polymorphic_identity = "some_identity"
mapper.polymorphic_map["some_identity"] = mapper
# in 0.8, might try to fix this for 0.9
mapper._identity_class = mapper.inherits._identity_class

this is something that can be supported as a feature so feel free to add a feature request.

Edit: here's a test using declarative:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import class_mapper

Base= declarative_base()


class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    type = Column(String)
    __mapper_args__ = {"polymorphic_on": type, "polymorphic_identity": "a"}

class B(A):
    pass

mapper = class_mapper(B)
mapper.polymorphic_identity = "b"
mapper.polymorphic_map["b"] = mapper

e = create_engine("sqlite://", echo=True)

Base.metadata.create_all(e)

s = Session(e)

s.add_all([A(), B(), B(), A(), A()])
s.commit()

s.close()

assert [obj.type for obj in s.query(B).order_by(A.id)] 
            == ['b', 'b']

assert [type(obj) for obj in s.query(B).order_by(A.id)] 
            == [B, B]

assert [obj.type for obj in s.query(A).order_by(A.id)] 
            == ['a', 'b', 'b', 'a', 'a']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...