-
-
Save Kroisse/1441484 to your computer and use it in GitHub Desktop.
Class Factory??
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
tag_types = ['location', 'category'] | |
class Tag(db.Model, BaseEntityMixIn, | |
IgnoreDeletionMixIn): | |
__tablename__ = "tag" | |
name = db.Column(db.Unicode(255), index=True) | |
type = db.Column(db.Enum(*tag_types, native_enum=False), index=True) | |
__mapper_args__ = {'polymorphic_on': type} | |
def __build_sub_tag(tag_type): | |
class SubTag(Tag): | |
__tablename__ = None | |
__mapper_args__ = {'polymorphic_identity': tag_type} | |
def __init__(self, **kwargs): | |
kwargs["type"] = tag_type | |
Tag.__init__(self, **kwargs) | |
return SubTag | |
g = globals() | |
for name in tag_types: | |
g[name.capitalize() + 'Tag'] = __build_sub_tag(name) |
다른 모듈에서 import할때도 정확히 먹힐지는 잘 모르겠네요;
repl에서 임포트해서 써봤는데 잘 됩니다. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
globals() 을 생각 못하고 있었는데 이거 좋네요.