Skip to content

Instantly share code, notes, and snippets.

@longfin
Created December 7, 2011 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save longfin/1441283 to your computer and use it in GitHub Desktop.
Save longfin/1441283 to your computer and use it in GitHub Desktop.
Class Factory?
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}
g = globals()
for t in TAG_TYPES:
def __init__(self, t=t, **kwargs):
kwargs["type"] = t
Tag.__init__(self, **kwargs)
cls_name = t.capitalize()+"Tag"
cls = type(cls_name, (Tag,), {'__tablename__':None,
'__mapper_args__': {'polymorphic_identity' : t},
'__init__': __init__})
g[cls_name] = cls
@Kroisse
Copy link

Kroisse commented Dec 7, 2011

SQLAlchemy의 냄새가 강하게 느껴지네요... 그런데 뭔가 다른 확장을 같이 쓰고 계신 건가요?

@longfin
Copy link
Author

longfin commented Dec 7, 2011

예. flask-sqlalchemy를 덮어씌워 쓰고는 있는데 저기선 드러날게 없을거고... BaseEntityMixIn이나 IgnoreDeletionMixIn은 자체적으로 만든 믹스인이죠.

@longfin
Copy link
Author

longfin commented Dec 7, 2011

생각해보니까 Tag.type을 서브타입 추가할때마다 자동으로 확장되게 해야겠네....

@Kroisse
Copy link

Kroisse commented Dec 7, 2011

flask-sqlalchemy라니!
다른건 아니고 SQLAlchemy를 날로 쓰면 보통 db.~~~ 같은 게 안 붙어서 혹시나 했어요.

@Kroisse
Copy link

Kroisse commented Dec 7, 2011

https://gist.github.com/1441484 이런 짓을 하면... 안되겠죠 =3

@longfin
Copy link
Author

longfin commented Dec 7, 2011

SAWarning이 거슬려서 type()을 쓰게끔 대대적으로 변경...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment