【发布时间】:2012-09-16 23:41:36
【问题描述】:
我正在编写 ImgProperty,我想在其中为用户提供一种定义默认高度和宽度的方法,如下所示:
class MyModel(ndb.Model):
img = ImgProperty(height=32, width=32)
然后:
m = MyModel(img='https://example.com/my-photo')
然后图像将保存到调整大小为给定高度、宽度值的数据库中。
ImgProperty 本身是 BlobProperty 的子类。我可以将这两个属性添加到 Property._attributes 并使其工作吗?还是最好不要惹它?我看到的另一种方法是创建一个具有字段高度和宽度的中间模型,并将__init__ 方法添加到 ImgProperty。
像这样的:
class ImgModel(ndb.Model):
height = ndb.IntegerProperty()
width = ndb.IntegerProperty()
class ImgProperty(ndb.BlobProperty):
def __init__(self, **kwds):
super(ImgProperty, self).__init__(ImgModel, **kwds)
我不确定这种方式是否允许像 img = ImgProperty(height=32, width=32) 这样的定义。
【问题讨论】:
标签: google-app-engine python-2.7 app-engine-ndb