使用ModelMixin来重用模型
2010 7 26 06:28 PM 1505次查看
分类:Google App Engine 标签:Google App Engine, Python
简单来说,不能重用的原因是__metaclass__不一致,所以只要自己再创建一个ModelMixin类用来继承即可:
class ModelMixin(object):
__metaclass__ = db.PropertiedClass
@classmethod
def kind(self):
"""Need to implement this because it is called by PropertiedClass
to register the kind name in _kind_map. We just return a dummy name.
"""
return '__model_mixin__'
接下来就是继承和复用了:class DateMixin(ModelMixin):
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
class AuditMixin(ModelMixin):
created_by = db.UserProperty()
updated_by = db.UserProperty()
class Account(db.Model, DateMixin, AuditMixin):
name = db.StringProperty()
class SupportTicket(db.Model, DateMixin, AuditMixin):
title = db.StringProperty()
class Item(db.Model, DateMixin):
name = db.StringProperty()
description = db.StringProperty()
0条评论 你不来一发么↓